We are trying to commonize almost duplicate tests using each
as shown below.
RSpec.describe Hoge, type::model do
describe'describe1'do
[create(:foo), create(:bar)].each do|v|
describe"#{v.name}"do
# Test with v
end
end
end
end
In this case, the following error will not work.
from/Users/shingo/Documents/raku/project/spec/models/hoge_spec.rb:53:in `block(3 levels) in<top(required)>'
from/Users/shingo/Documents/raku/project/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb: 385:in`module_exec'
from/Users/shingo/Documents/raku/project/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb: 385: in `subclass'
from/Users/shingo/Documents/raku/project/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb: 255: in `block in define_example_group_method'
from/Users/shingo/Documents/raku/project/spec/models/hoge_spec.rb: 36: in `block(2 levels) in<top(required)>'
from/Users/shingo/Documents/raku/project/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb: 385:in`module_exec'
from/Users/shingo/Documents/raku/project/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb: 385: in `subclass'
from/Users/shingo/Documents/raku/project/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb: 255: in `block in define_example_group_method'
from/Users/shingo/Documents/raku/project/spec/models/hoge_spec.rb:35:in `block in<top(required)>'
from/Users/shingo/Documents/raku/project/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb: 385:in`module_exec'
from/Users/shingo/Documents/raku/project/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb: 385: in `subclass'
from/Users/shingo/Documents/raku/project/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb: 255: in `block in define_example_group_method'
from/Users/shingo/Documents/raku/project/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/dsl.rb:43: in `block in export_example_group_alias'
from/Users/shingo/Documents/raku/project/spec/models/hoge_spec.rb:26:in `<top(required)>'
How can each
make it common?
Have you forgotten to write it? In addition, it is not a way to use each, but if you want to make it common, I think it is better to use shared_examples or shared_context.
Introduction to and beyond RSpec, 2nd small version~RSspec3 version~-Qiita
In this case, would it be like the following?
RSpec.describe Hoge, type::model do
shared_examples "describe1" do
it do
# Test with v
end
end
context "foo" do
let(:v) {create:foo}
it_behaves_like 'describe1'
end
context "bar" do
let(:v) {create:bar}
it_behaves_like 'describe1'
end
end
© 2024 OneMinuteCode. All rights reserved.