Trying to set up a quick POC locally. I have a pa...
# pact-ruby
s
Trying to set up a quick POC locally. I have a pact json file in my rails app and have followed the guide. 1. Should I be running the rake command against
test
or
development
?
Copy code
bundle exec rake pact:verify                                                                                                                                                              
rake aborted!
KeyError: key not found: "RAILS_ENV"
2. The API request in the consumer pact is a GET request to get foo/123. How do I ensure this id and associated data will exist in the database? (either test or dev). Surely I don't create this data but if I need do how is this done? I was under the impression that Provider simply pulls down the contract and runs it against an instance of the app. Since this will run on CI then I assume it will run against
test
and out sqlite3 db?
Just to follow up on this. Our consumer is Java based so it using v4 of the pact specification, but they are able to generate version 3. it looks like ruby only supports v2, so a lot of warnings are generated, e.g.
WARN: Ignoring unsupported combine AND for path
in terms of ensuring the test data is there, I am using the provider_states_for and inserting into the test database. The only way I can get this to work is by using the:
Copy code
setup_do
... // using factorybot to create the item in sqlite3
end
if i tried to use:
Copy code
provider_state "there is a foo named test foo" do
  set_up do
  .. // using factorybot to create the item in sqlite3
  end
end
it never works, the code in the provider state never gets called. I assumed it was some naming issue, in that the
providerState
field in the pact contract json needed to match the
provider_state
in the provider rb file. Both are exactly called
there is a foo named test foo
so i am not sure what the issue could be? The pact provider and consumer names also match up and are the same in the json file and the pact_helper.rb etc i am using the pact 1.64.0 ruby gem. ``````
y
hey up i think pact jvm can also generate v2 specs which are fully supported by the ruby impl. provider state key was renamed from v2 -> v3 for test data, you would use provider states, where your provider would be responsible for honouring the state and setting the up the pre requisite data https://docs.pact.io/getting_started/provider_states
s
Thanks - I will see if they can generate the v2 specs. Any ideas why the
provider_state
is not working? I have to use the base state to setup the data, but this is less than ideal. No matter what I try, the provider_state block never gets executed
Copy code
require 'factory_bot_rails'
include FactoryBot::Syntax::Methods

Pact.provider_states_for 'GEB' do

  provider_state "there is an Thing named Test Thing" do
    set_up do
      Thing.delete_by(id: 1)
      create(:thing, id: 1, name: 'Test Thing', postal_addresses: [create(:postal_address, country_id: 1, postal_zip_code: "abc123")])
    end

    tear_down do
      # Any tear down steps to clean up the provider state
    end
  end

  provider_state "a thing does not exist" do
    no_op # If there's nothing to do because the state name is more for documentation purposes,
    # you can use no_op to imply this.
  end

end