转自  http://lmws.net/describe-vs-context-in-rspec

学习rspec,不太理解describe和context。google了一下,找到这篇文章,感觉说的有些道理

1 在Rspec的世界里,我们经常看到人们使用descirbe代码块和context代码块 例如

 describe "launch the rocket" do
context "all ready" do
end context "not ready" do
end
end

那么context和descript有啥不同呢

According to the rspec source code, “context” is just a alias method of “describe”, meaning that there is no functional difference between these two methods. However, there is a contextual difference that’ll help to make your tests more understandable by using both of them.

说的是,按照rspec源码来看,context仅仅是describe方法的别名,意味着这两者没有任何功能性的不同, 然而有一些语境不同(语义不同),理解这个能让你写出更可读的代码

The purpose of “describe” is to wrap a set of tests against one functionality while “context” is to wrap a set of tests against one functionality under the same state. Here’s an example

describe的目的是当它包裹一组,一个功能,的测试代码

context的目的是包裹一组,在同一状态下,一个功能的,测试代码

例如下面代码

 describe "launch the rocket" do
before(:each) do
#prepare a rocket for all of the tests
@rocket = Rocket.new
end context "all ready" do
before(:each) do
#under the state of ready
@rocket.ready = true
end it "launch the rocket" do
@rocket.launch().should be_true
end
end context "not ready" do
before(:each) do
#under the state of NOT ready
@rocket.ready = false
end it "does not launch the rocket" do
@rocket.launch().should be_false
end
end
end

上面的代码可读性好在,没有将所有东西都用describe包裹,因为你读测试代码是在context语境内,对于一个context设置一个对象状态,

@rocket.ready为true时, @rocket.launch() 的行为,导致一个结果,如果只关注一个这个状态时,火箭的启动结果,就不需要再看其他状态的代码

Rspec中describe和context不同的更多相关文章

  1. android开发中fragment获取context

    在用到fragment时无法使用.this来指定当前context内容,android开发中fragment获取context,可以使用getActivity().getApplicationCont ...

  2. Android应用开发中关于this.context=context的理解

    在Android应用开发中,有的类里面需要声明一个Context的成员变量,然后还需要在该类的构造函数中加上this.context=context;这行代码.为什么要这么写呢?不写不行么? 先看下面 ...

  3. HttpHandler中无法使用Session(context.Session 为Null)

    一.问题描述: 最近需要做一个对特定请求(.report)进行响应的接口,当然是使用实现IHttpHandler来进行处理,实现IHttpHandler接口就必须要实现它的两个方法,ProcessRe ...

  4. Spring.xml中配置注解context:annotation-config和context:component-scan简述

    XML中context:annotation-config和context:component-scan简述 <context:annotation-config/> 中文意思:<上 ...

  5. mysql中describe关键字

    今天写代码的时候,有一个类的数据始终不能插入,老是提示在You have an error in your SQL syntax; check the manual that corresponds ...

  6. android中LayoutInflater.from(context).inflate的分析

    在应用中自定义一个view,需要获取这个view的布局,需要用到 (LinearLayout) LayoutInflater.from(context).inflate(R.layout.conten ...

  7. rspec中的let和let!区别

    文档 https://relishapp.com/rspec/rspec-core/v/2-5/docs/helper-methods/let-and-let 从上面文档中得出 let 1 只会在一个 ...

  8. spring中注解注入 context:component-scan 的使用说明

    通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个标签,配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,如果扫描 ...

  9. 深入理解 Android 中的各种 Context

    前言 网上关于 Context 的文章也已经有不少了,比如值得参考的有: Android Context完全解析,你所不知道的Context的各种细节 Android Context 到底是什么? 但 ...

随机推荐

  1. OSGi 的核心配置、动态化及问题

    一.OSGi的核心组件Bundle,与java中jar包的差别就是元数据配置:      常用的Bundle元数据定义:     a) Bundle-Activator:定义Activator的实现全 ...

  2. log4j 使用笔记整理中

    Log4j由三个重要的组件构成:日志信息的优先级,日志信息的输出目的地,日志信息的输出格式. 日志信息的常用的优先级从高到低有ERROR.WARN. INFO.DEBUG,分别用来指定这条日志信息的重 ...

  3. Python--类定义

    转自:http://kanwoerzi.iteye.com/blog/1304466 Python笔记——类定义 一.类定义: class <类名>: <语句> 类实例化后,可 ...

  4. sql server 创建文件组,文件

    添加文件组:  --ADD FILEGROUP 增加文件组 ALTER DATABASE TestHekaton ADD FILEGROUP [Report] ALTER DATABASE TestH ...

  5. Cannot unwrap to requested type [javax.sql.DataSource]

    遇上这个bug我的情况是这样,hibernate4以后,spring3.1不再有hibernateDaoSupport,在dao层不能继承HibernateDaoSupport, 只能显式声明Sess ...

  6. linux包之iproute之ip命令

    [root@localhost ~]# rpm -qf /sbin/ipiproute-2.6.32-31.el6.x86_64ip 是个命令, ip 命令的功能很多!基本上它整合了 ifconfig ...

  7. 创建对象的最好方式&最好的继承机制(代码实例)

    /* 创建对象的最好方式:混合的构造函数/原型方式, *用构造函数定义对象的所有非函数属性,用原型方式定义对象的函数属性(方法) */ function People(sname){ this.nam ...

  8. DataList与Repeater嵌套绑定

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="home.aspx.cs&quo ...

  9. 微信公众号开发笔记(C#)

    这篇文章还不错,使用  .net , 对微信用户的想公众号发送的文字进行回复.比较简单,自己可以修改更复杂的回复. 微信公众号开发笔记(C#) 原文地址 需求分析 根据用户在微信上发送至价值中国公众号 ...

  10. ActionScript ArrayCollection sort

    var sortByOrderId:Sort = new Sort; sortByOrderId.fields = [new SortField("orderId")]; orde ...