Search code examples
ruby-on-railstddminitest

Mini Test and Rails 6: Re-use variables on different tests


How do you declare a variable that can persist on different tests? For example I have the following

setup do 
  @payload = {...}
  @another_payload = {...}
end

on one controller, I end up copying them to a different controller if I need to use them, is there a way to make them persist across tests?


Solution

  • You could use Concern to do this:

    # test/supports/payload_setup.rb
    module PayloadSetup
      extend ActiveSupport::Concern
    
      included do
        setup do 
          @payload = {...}
          @another_payload = {...}
        end
      end
    end
    
    # some_test.rb
    class SomeTest < ActiveSupport::TestCase
      include PayloadSetup
    
      test 'some test' do
        ...
      end
    end