嵌套上下文可以帮助你在这里,但保持浅(一般为1级深)。在每个示例中都有两个变量需要考虑:givens(起始状态)和正在调用的方法。您可以通过法或国家集团的事情:
# by method
describe Stack do
describe "#push" do
it "adds an element to an empty stack"
it "adds an element to a non-empty stack"
end
describe "#pop" do
it "returns nil from an empty stack"
it "returns the last element of a non-empty stack"
it "removes the last element from a non-empty stack"
end
end
# by state
describe Stack do
context "when empty" do
specify "push adds an element"
specify "pop returns nil"
end
context "when not empty" do
specify "push adds an element"
specify "pop returns last element"
specify "pop removes last element"
end
end
我用这两种方法,看到他们两个的工作真的很好,真的很厉害。任何一种方法的关键在于,这些例子在你从上到下阅读时会讲述一个故事。随着需求的发展,这意味着您需要查看此文件,就像您执行代码一样。 一个简单的方法来检查规范有意义是与文档格式运行:
rspec stack_spec.rb --format documentation
该吐出所有的名字,以便(只要你不使用--order兰特):
Stack
#push
adds an element to an empty stack
adds an element to a non-empty stack
#pop
returns nil from an empty stack
returns the last element of a non-empty stack
removes the last element from a non-empty stack
或
Stack
when empty
push adds an element
pop returns nil
when not empty
push adds an element
pop returns last element
pop removes last element
一旦你看到这个输出,您正在使用,使组织是否感觉到与否这将是很清楚的给你。
我通常把一切都在一个单独的文件和折叠/展开块好一点导航代码。这是一个巨大的文件的痛苦,但我从来没有与我懒惰分开在类/实例方法,回调/验证等测试... – apneadiving 2012-02-10 13:16:38
我想你可以使用include和mixin每个文件到基地这种方式。按照约定在目录结构中按类/子类组织文件。我认为这是我会做的,再加上大卫的回答如下。感谢杀手问题! – ipd 2012-09-28 17:00:55