基本结构(describe
/it
)
RSpec 是一种用于创建代码预期行为的可执行示例的 DSL,这些示例按组组织。它使用“describe”和“it”这些词,这样我们就可以像对话一样表达概念。
"Describe an account when it is first opened."
"It has a balance of zero."
describe
方法创建一个示例组。在传递给describe
的块中,您可以使用describe
或context
方法声明嵌套组,或者您可以使用it
或specify
方法声明示例。
在后台,一个示例组是一个类,其中传递给describe
或context
的块被评估。传递给it
的块在该类的实例上下文中被评估。
一个组,一个示例
给定一个名为“sample_spec.rb”的文件,其中包含
RSpec.describe "something" do
it "does something" do
end
end
当我运行rspec sample_spec.rb -fdoc
然后输出应包含
something
does something
嵌套示例组(使用context
)
给定一个名为“nested_example_groups_spec.rb”的文件,其中包含
RSpec.describe "something" do
context "in one context" do
it "does one thing" do
end
end
context "in another context" do
it "does another thing" do
end
end
end
当我运行rspec nested_example_groups_spec.rb -fdoc
然后输出应包含
something
in one context
does one thing
in another context
does another thing