pact-foundation / pact-ruby

Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
https://pact.io
MIT License
2.17k stars 215 forks source link

`pact_dir` setting ignored #184

Closed jrmhaig closed 5 years ago

jrmhaig commented 5 years ago

I am using Pact with Minitest and so I want to change the location where the contracts are created to test/pacts. The documentation states that I can do this with:

Pact.configure do |config|
  config.pact_dir = './test/pacts'
end

However, I have found that this will create the directory test/pacts but then save the contracts in spec/pacts anyway. I have a minimal example below:

require 'minitest/autorun'
require 'pact/consumer/minitest'
require 'httpclient'

class PactTest < Minitest::Test
  include Pact::Consumer::Minitest

  Pact.service_consumer 'consumer' do
    has_pact_with 'provider' do
      mock_service :provider do
        port 1234
      end
    end
  end

  Pact.configure do |config|
    config.pact_dir = './test/pacts'
  end

  def test_pact
    provider
      .given('data')
      .upon_receiving('a request')
      .with(method: :get, path: '/')
      .will_respond_with(status: 200)

    HTTPClient.new.get_content('http://localhost:1234')
  end
end

This produces:

$ ls test/pacts
ls: test/pacts: No such file or directory
$ ls spec/pacts
ls: spec/pacts: No such file or directory
$ bundle exec ruby test/pact_test.rb
Run options: --seed 56048

# Running:

.

Finished in 0.013516s, 73.9864 runs/s, 0.0000 assertions/s.

1 runs, 0 assertions, 0 failures, 0 errors, 0 skips
$ ls test/pacts
$ ls spec/pacts
consumer-provider.json

The versions of the gems I am using are:

$ bundle show
Gems included by the bundle:
  * awesome_print (1.8.0)
  * bundler (1.16.4)
  * diff-lcs (1.3)
  * filelock (1.1.1)
  * find_a_port (1.0.1)
  * httpclient (2.8.3)
  * json (2.1.0)
  * minitest (5.11.3)
  * pact (1.36.0)
  * pact-consumer-minitest (1.0.1)
  * pact-mock_service (2.12.0)
  * pact-support (1.8.0)
  * rack (2.0.5)
  * rack-test (0.8.3)
  * randexp (0.1.7)
  * rspec (3.8.0)
  * rspec-core (3.8.0)
  * rspec-expectations (3.8.2)
  * rspec-mocks (3.8.0)
  * rspec-support (3.8.0)
  * term-ansicolor (1.6.0)
  * thor (0.20.0)
  * tins (1.17.0)
  * webrick (1.4.2)
bethesque commented 5 years ago

You need to configure your pact directory before you define your service consumer.

jrmhaig commented 5 years ago

Thank you for the clarification. As I have the Pact.service_consumer in a separate file it was being required at the top of test_helper.rb before the configuration. I've rearranged things and it is all good now.