thoughtbot/shoulda-context

Repeating Tests in Multiple Contexts

Closed this issue · 3 comments

I run into this issue a lot and haven't figured out the best and most beautiful way to avoid repeating complicated code (I've posted here on Stack Overflow as well). Here is the problem I have the exact same tests that I want to run in multiple contexts. What is the best way to do this?

context 'first context' do
   setup do
      # Setup
   end

   should 'do something' do
     ...
   end

   should 'do something else' do
     ...
   end
end

context 'second context' do
   setup do
      # Different setup
   end

   should 'do something' do
     ...
   end

   should 'do something else' do
     ...
   end
end

@tomrossi7 did you come up with a solution for this? Having the same problem right now...

ok, I got it to work now with an old school makro ( https://robots.thoughtbot.com/shoulda-matchers#old-school-macros )

I just put the makro on top of the test and referenced it in the different contexts. Seems the order really mattered here. First had it at the bottom which did not work.

Closing this issue since there seems to be a workaround for this.