I want to use FactoryGirl data to spin the RSpec description.

Asked 2 years ago, Updated 2 years ago, 131 views

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?

ruby-on-rails rspec

2022-09-29 21:34

1 Answers

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


2022-09-29 21:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.