0

Just playing around with Pact against my Rails API and noticed that the out-of-the-box Pact setup runs against the "development" environment by default.

How do I configure to run against the "test" environment without having to specify it in command line when I run the task (RAILS_ENV=test). Couldn't find it easily in the docs how to do it.

Using following gems:

pact (1.10.0)
pact-mock_service (0.12.1)
pact-support (0.6.0)

pact_helper.rb:

require 'pact/provider/rspec'

Pact.service_provider 'Auslan API Service' do
  honours_pact_with 'Auslan Web App' do
    # This example points to a local file, however, on a real project with a continuous
    # integration box, you would use a [Pact Broker](https://github.com/bethesque/pact_broker) or publish your pacts as artifacts,
    # and point the pact_uri to the pact published by the last successful build.
    pact_uri './user-specs-user-api.json' # need to update this
  end
end

Pact.configure do | config |
  config.diff_formatter = :embedded
end

Pact.provider_states_for 'User-Specs' do
  provider_state 'there are users already added inside the database' do
    set_up do
      user1 = User.create(email: 'abcd@a.au', first_name: 'Jane', last_name: 'Doe', password: 'abcd#1234')

      # set the Auth token
      token = Knock::AuthToken.new(payload: { sub: user1.id }).token   
      pacts = File.join(File.dirname(File.expand_path(__FILE__)), '../../user-specs-user-api.json')

      Dir.glob(pacts).each do |f|
        text = File.read(f)
        output_of_gsub = text.gsub(/\"Authorization\"\s*:\s*\".+\"/) { "\"Authorization\": \"Bearer #{token}\"" }
        File.open(f, "w") { |file| file.puts output_of_gsub }
      end
    end
  end
end

Thanks, Mo

1 Answers1

1

I haven't written any code to allow that to happen. The part of the code where the app gets loaded is here: https://github.com/pact-foundation/pact-ruby/blob/master/lib/pact/provider/configuration/service_provider_dsl.rb#L16

You can override the app in the configuration if you have a handle to it, but I can't remember how to do that with with a Rails app off the top of my head. Maybe you could have a play around with the Rack builder and see if you can pass in any environment variables to it. I'd be happy to accept a PR if you can work out how to do it.

laverya
  • 242
  • 4
  • 12
Beth Skurrie
  • 1,333
  • 7
  • 8