转自  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. mysql学习之-逻辑架构

    mysql逻辑架构图: 第一层,服务层(为客户端服务):为请求做连接处理,授权认证,安全等. 1.  每个连接的查询都在一个进程中的线程完成. 2.  服务器负责缓存线程,所以服务层不需要为每个连接新 ...

  2. asp.net脚本获取不到id,服务器控件id生成html页面id控制

    在配置文件里修改id的生成方式.经过搜索发现是因为使用Sharpmap而在Web.config中加入的一句配置造成的影响. <pages controlRenderingCompatibilit ...

  3. 基于LBS的地理位置附近的搜索以及由近及远的排序

    Nosql学习之Redis资料(一) http://redis.io/download 目前基于LBS地理位置的搜索已经应用非常广了,的确是个很方便的东西. 我们做程序的就是要考虑如何通过这些功能,来 ...

  4. 如何计算oracle数据库内存

    数据库内存设置: 项目 数据关系 单位 系统CPU n 个 物理内存Memory 假设4G物理内存 4*1024 MB memory_target 0.5*4*1024 0.5*Memory sga_ ...

  5. mybatis——动态sql

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...

  6. XAMPP 在windows下无法启动Apache解决方案

    XAMPP 在windows下无法启动Apache解决方案 一.现象 XAMPP 点击Start Apache时出现如下错误 20:41:12  [Apache] Error: Apache shut ...

  7. C# .NET MVC 接收 JSON ,POST,WCF 无缝隙切换

    用来与HTTP 模式的WCF 无缝隙切换. 服务端是MVC. 在Home/About 上面加 [HttpPost].读取Request.InputStream 中所有内容.然后返回JSON给客户端:r ...

  8. 剑指offer系列55---最小的k个数

    [题目] 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. *[思路]排序,去除k后的数. package com.exe11 ...

  9. PHP替换,只替换匹配到的第一个

    function str_replace_limit($search, $replace, $subject, $limit=-1) { if (is_array($search)) { foreac ...

  10. flash builder 启动ios模拟器失败是什么原因?

    参考知乎:http://www.zhihu.com/question/22537362 在mac os设置-安全性与隐私-隐私-辅助功能 找到flash bulder 打上前面的勾,如下图: