下記のようにeach
を使ってほとんど重複するテストを共通化しようとしています。
RSpec.describe Hoge, type: :model do
describe 'describe1' do
[create(:foo), create(:bar)].each do |v|
describe "#{v.name}" do
# vを使ってテスト
end
end
end
end
この場合、下記のようなエラーになりうまくいきません。
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 expose_example_group_alias'
from /Users/shingo/Documents/raku/project/spec/models/hoge_spec.rb:26:in `<top (required)>'
どのようにすれば、each
で共通化できるのでしょうか?
describe "#{v.name}"
の後ろにdo
がありませんがtypoでしょうか?