转自  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. Postfix性能测试(PHP版)

    Postfix的性能压测(PHP版) 发送测试:分别使用PHP Mail()函数和PHPMailler smtp协议发送邮件, 推送速率是指 发送个数/PHP程序运行时间, 发送速率是指 发送个数/( ...

  2. 用一条UPDATE语句交换两列的值

    在SQL UPDATE语句中,"="右侧的值在整个UPDATE语句中都是一致的,所有更新同时发生!因此以下语句将在没有临时变量的情况下交换两列的值: UPDATE table SE ...

  3. System.Web.HttpContext.Current 跟踪分析

    public static HttpContext Current { get { return ContextBase.Current as HttpContext; } set { Context ...

  4. how-to-install-hyper-v-on-a-virtual-machine-in-hyper-v.aspx

    BEFORE STARTING IT IS IMPORTANT TO KNOW THAT THIS ENVIRONMENT IS NOT SUPPORTED BY MICROSOFT. USE IT ...

  5. Q6: Binary Tree Preorder Traversal

    问题描述 Given a binary tree, return the preorder traversal of its nodes' values. For example:Given bina ...

  6. JS 阻止浏览器默认行为和冒泡事件

    JS 冒泡事件   首先讲解一下js中preventDefault和stopPropagation两个方法的区别: preventDefault方法的起什么作用呢?我们知道比如<a href=& ...

  7. BlueStacks 设置代理服务器 Proxifier指定任意程序的代理服务器

    详见地址: http://www.ccproxy.com/proxifier-tou-ming-dai-li.htm BlueStacks如何使用代理服务器 http://www.360doc.com ...

  8. 阿里云centos增加swap(虚拟内存)

    我的阿里云服务器默认是没有配swap交换文件. 创建swap根目录 mkdir /swap 创建swap dd if=/dev/zero of=/swap/swap bs=1024 count=102 ...

  9. C#执行DOS命令(CMD命令)

    在c#程序中,有时会用到调用cmd命令完成一些功能,于是在网上查到了如下方法,实现了c#执行DOS命令,并返回结果.         //dosCommand Dos命令语句         publ ...

  10. asp.net生成PDF文件 (1)

    asp.net生成PDF文件 (1) 这个是例子是网上淘来的,哈哈,很有用的! 首先要到网上下载itextsharp.dll,然后添加引用,主程序如下: 1 2 3 4 5 6 7 8 9 10 11 ...