例:
文件:spec / test_spec.rb
describe 'some thing' do let(:some_feature) { true } describe 'some tests' do include_examples "shared_tests" endend
文件规范/共享/ shared_tests.rb
shared_examples_for "shared_tests" do include_examples "feature_specific_tests" if some_featureend
正如预期的那样,这会抛出这样的错误:
undefined local variable or method `some_feature`
有没有办法做到这一点?我想也许我可以在before(:all)块中定义@some_feature,然后在shared_examples中使用if @some_feature,但这总是为零.
解决方法 重写答案,使其更清晰:你有这个:
文件:spec / test_spec.rb
describe 'some thing' do let(:some_feature) { true } describe 'some tests' do include_examples "shared_tests" endend
文件规范/共享/ shared_tests.rb
shared_examples_for "shared_tests" do include_examples "feature_specific_tests" if some_featureend
将其更改为:
文件:spec / test_spec.rb
describe 'some thing' do describe 'some tests' do include_examples "shared_tests" do let(:some_feature) { true } end endend
文件规范/共享/ shared_tests.rb
shared_examples "shared_tests" do if some_feature it_should_behave_like "feature_specific_tests" end # rest of your tests for shared example group # 'a logged in registered user goes hereend
这一切都很好用:-)
总结以上是内存溢出为你收集整理的ruby – 包含带变量的Rspec示例全部内容,希望文章能够帮你解决ruby – 包含带变量的Rspec示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)