Dynamically generating multiple test cases

48 views
Skip to first unread message

siva

unread,
Aug 14, 2014, 5:28:15 AM8/14/14
to rs...@googlegroups.com

  Hi 

  # spec/support/generate_test_cases.rb

 module RSpec
  module GenerateTestCases

    def generate_test_cases
      TestGenerator.new.generate_test_cases
    end 

    class TestGenerator < RSpec::Core::ExampleGroup
      def generate_test_cases
        3.times do  
          TestGenerator.describe "Test Cases" do  
            example do  
              expect(1+1).to eq(2)
            end 
          end 
        end 
      end 
    end 
  end 
end

RSpec.configure do |rspec|
  rspec.extend RSpec::GenerateTestCases
end    

RSpec::SharedContext.send(:include, RSpec::GenerateTestCases)

# spec/models/user_spec.rb

RSpec.describe "User" do
    generate_test_cases
end


When I run above test cases it shows 0 examples to run. What I am doing wrong? How can I generate multiple test cases? Could help me? 

Thanks in advance. 
~                                                            

Jon Rowe

unread,
Aug 14, 2014, 7:37:45 AM8/14/14
to rs...@googlegroups.com
This doesn’t work because you’re not registering the example groups you’re creating with, a simpler way to do this would be:

 # spec/support/generate_test_cases.rb

 module RSpec
  module GenerateTestCases

    def generate_test_cases
      TestGenerator.new(self).generate_test_cases
    end 

    class TestGenerator < Struct.new(:rspec)
      def generate_test_cases
        3.times do  
          rspec.describe "Test Cases" do  
            example do  
              expect(1+1).to eq(2)
            end 
          end 
        end 
      end 
    end 
  end 
end

Jon Rowe
---------------------------

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/cb8b6886-7f33-4dab-870d-73495262c792%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

siva

unread,
Dec 11, 2014, 9:49:15 AM12/11/14
to rs...@googlegroups.com
 

Hi Jon Rowe

 # spec/requests/sample_spec.rb

 3.times do 
   example "expected 1 + 1 should eq to 2" do
     expect(1+1).to eq(2)
   end
 end

When I ran above code it generated 3 test cases. How this is different from above code? Could please explain?

Jon Rowe

unread,
Dec 11, 2014, 3:55:26 PM12/11/14
to rs...@googlegroups.com
When you do `3.times` in an existing spec you’re using an already setup `ExampleGroup` instance which links into the reporter.

With your code you create an isolated example group and use that to create your examples meaning they have no link to the current setup or reporter.

My code restores that link by using the existing example group as a generator.

Jon Rowe
---------------------------

siva

unread,
Dec 31, 2014, 8:14:34 AM12/31/14
to rs...@googlegroups.com
Hi Jon Rowe

I didn't understanding these two lines:

  With your code you create an isolated example group and use that to create your examples meaning they have no link to the current setup or reporter.

  My code restores that link by using the existing example group as a generator.

Could you please elaborate? 

Jon Rowe

unread,
Jan 1, 2015, 4:34:05 AM1/1/15
to rs...@googlegroups.com
I'll try, basically your create a new instance that's not setup properly, my example uses an existing setup instance.
Reply all
Reply to author
Forward
0 new messages