下記のように`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`で共通化できるのでしょうか？
CC BY-SA 3.0
2016-07-13T01:22:39.053
27586
2016-07-14T05:27:34.840
2016-07-13T08:45:38.707
9008
9008
question
ruby-on-rails rspec
RSpecのdescribeをFactoryGirlで作ったデータで回したい
it を書き忘れていたりしないでしょうか？ なお、each を使う方法にはなっていませんが、共通化するなら、shared_examples や
shared_context を使のが良いと思います。

[RSpec の入門とその一歩先へ、第2イテレーション ～RSpec 3バージョン～ -
Qiita](http://qiita.com/jnchito/items/47f539ddca7dbc01acef#shared_examples-%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E3%83%86%E3%82%B9%E3%83%88%E3%81%AE%E9%87%8D%E8%A4%87%E3%82%92%E6%8E%92%E9%99%A4%E3%81%99%E3%82%8B)

今回のケースなら、下記のような感じでしょうか。

```

    RSpec.describe Hoge, type: :model do
      shared_examples "describe1" do
        it do 
          # 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
    
```
CC BY-SA 3.0
2016-07-14T05:27:34.840
27612
2016-07-14T05:27:34.840
7471
27586
answer