分享的例子
和共享上下文
之间的真正区别是什么?
我的观察:
我可以用这两种方法测试相同的东西(即
分享的例子
或共享上下文
)但如果我使用后面的测试,我的其他一些测试就会失败.
Observation #1 :
我比较了https://www.relishapp.com/份文档中的分享的例子份和共享上下文份
句法上的差异是:
- 共享上下文 to define a block that will be evaluated in the context of example groups by implicitly matching metadata
例子:
共享上下文 "shared stuff", :a => :b do
...
end
- 从测试文件中包含或调用它们的方式
分享的例子
include_examples "name" # include the examples in the current context
it_behaves_like "name" # include the examples in a nested context
it_should_behave_like "name" # include the examples in a nested context
共享上下文
include_context "shared stuff"
Observation #2
我有一个测试用例
共享上下文 'limit_articles' do |factory_name|
before do
@account = create(:account)
end
it 'should restrict 3rd article' do
create_list(factory_name, 3, account: @account)
article4 = build(factory_name, account: @account)
article4.should be_invalid
end
it 'should allow 1st article' do
...
end
it 'should allow 2nd article' do
...
end
end
And include the context in a spec file which already has one 共享上下文 included, then the existing one fails. But I change the order then all my test passes
Fails
include_context 'existing_共享上下文'
include_context 'limit_articles'
Also if I replace the 共享上下文
with 分享的例子
and accordingly include it in test case.
Passes
include_context 'existing_共享上下文'
it_behaves_like 'limit_articles'