原文:http://thoughtbot.com/projects/shoulda/tu×ial/context
上下文
在学习了Should语句之后,Shoulda的更多基本构建代码的知识就是上下文(Context)。上下文创建一个运行should语句的类似场景的fixture。Context代码块可以包含 setup/teardown 代码块,should代码块,或者其他context代码块。下面来试试在测试中使用简单的context:
class QueueTest < Test::Unit::TestCase
context "一个队列实例" do
setup do
@queue = Queue.new
end
should "响应 :push 调用" do
assert_respond_to @queue, :push
end
end
end
这里创建了一个名为”测试:一个队列实例可以响应:push调用”的测试方法,这很漂亮和易读。
嵌套
上面详尽的测试,简单测试队列的实例响应:push调用,但也提出了更多的上下文和测试。现在想看看让队列返回任何放进其中的东西。来添加一个嵌套的上下文进到其中:
class QueueTest < Test::Unit::TestCase
context "一个队列实例" do
setup do
@queue = Queue.new
end
should "响应 :push 调用" do
assert_respond_to @queue, :push
end
context "具有一个元素" do
setup { @queue.push(:something) }
should "在:pop调用后返回元素" do
assert_equal :something, @queue.pop
end
end
end
end
上面生成了测试方法”测试:一个队列实例应该响应:push调用”和”测试:一个队列实例具有一个元素应该在:pop调用后返回元素”。
注意一下,上下文的setup代码块是为每个should代码块运行一次的。首先@queue实例被创建出来,然后:something被放进去,接着执行assert_equal。下面用一个同样语意的测试方法虚拟地演示一下,记住下面的代码只是为了帮助理解,并不会实际的发生:
define_method "测试:一个队列实例有一个元素应该在:pop调用后返回元素。" do
@queue = Queue.new
@queue.push(:something)
assert_equal :something, @queue.pop
end
现在,如果使用一般的测试风格,就会在一个测试方法中,把一个元素放进队列中并在一个测试方法中把元素取出来,然后进行断言。但是将这个测试分离出来放到一个测试push的上下文时,就可以添加更多的共享同样setup的测试。
class QueueTest < Test::Unit::TestCase
context "一个队列实例" do
setup do
@queue = Queue.new
end
should "响应 [...]
分享到:
相关推荐
Shoulda是一个Rails插件,一个让Test::Unit也BDD的框架。
Shoulda可帮助您在Minitest和Test :: Unit下编写更易于理解,可维护的特定于Rails的测试。 快速链接 :loudspeaker: 概述 作为伞形宝石, shoulda宝石不包含任何自己的代码,而是带来了另外两个宝石的行为: 例如...
Shoulda- assign_to ( )在2.0.0版中删除了assign_to匹配器。 我们非常想念它,足以把它带回来。 该gem重新添加了assign_to匹配器,并通过引用包括了所有其他的shoulda-matchers就像它被无情地销毁之前一样。 示例...
shoulda-matchers, 提取的测试匹配器的集合应该应该 应该匹配 匹配器提供了RSpec-和minitest兼容的一个接口,用于测试公共 Rails 功能。 否则,这些测试将会更长。更复杂和容易出错。查看最新版本( 3.1.1 )的正式...
Shoulda-matchers( )在2.0.0版中删除了response_with_content_type匹配器。 我们非常想念它,足以把它带回来。 这个gem包含了所有当前版本的Shoulda-matchers,并重新添加了“ respond_with_content_type”匹配器...
shoulda-coulda假设您正在运行一个should-counter-api通过它可以保存和获取“ shoulds”。 Shoulda-coulda假定存在3个环境变量: SHOULDA_COULDA_TOKEN=the Slack access token for your bot's Slack SHOULD_...
Shoulda Matchers Shoulda Matchers提供了与RSpec和Minitest兼容的单线来测试通用的Rails功能,如果用手工编写的话,它们会更长,更复杂并且容易出错。快速链接 :open_book: 。 :loudspeaker: 。目录入门规范首先,...
React基于上下文的条件渲染组件以进行A / B实验 需要React v16.3.0及更高版本 npm i rebranch // example App.js import React from 'react' import { BranchProvider } from 'rebranch' // initialize the ...
lucene,lucene教程,lucene讲解。 为了对文档进行索引,Lucene 提供了五个基础的类 public class IndexWriter org.apache.lucene.index.IndexWriter public abstract class Directory org.apache.lucene.store....
2. Should we share our dreams for a better life with our parents or keep them to ourselves? 3. Can children ever understand their parents completely? 4. From the song can you guess what the theme of...
Why Should the United States Should Spread Democracy.pdf
超级详细的tkinker教程。Tkinter 是使用 python 进行窗口视窗设计的模块. 简单的构造, 多平台, 多系统的兼容性, 能让它成为让你快速入门定制窗口文件的好助手. 它在 python 窗口视窗模块中是一款简单型的. 所以用来...
Nowadays the newspaper possesses considerable value Everybody should read it. It supplies us with a variety of news every day. It tells us the political situation of the world. If we form the habit of...
97 Things Every Software Architect Should Know
2. Should we share our dreams for a better life with our parents or keep them to ourselves? 3. Can children ever understand their parents completely? 4. From the song can you guess what the theme of...
“I had a technical question about my project team here at Alpha ...“First projects typically should have a 50 percent improvement over the baseline sigma measure. If your baseline measure of
97 Things Every Programmer Should Know.doc
标准C语言基础教程 5The programs examined so far have been useful in illustrating the correct structure of C programs and in introducing fundamental C input, output, assignment, and selection ...
In no case should you give up.
97 Things Every Project Manager Should Know.pdf