https://pact.io logo
#general
Title
# general
s

Still

03/09/2022, 9:09 PM
Random question, but when we do contract testing, then we create the json files explicitly right. The response that we want, do we write that explicitly in json file before running the Pact test?
m

Matt (pactflow.io / pact-js / pact-go)

03/10/2022, 4:43 AM
No, the contract is generated by the act of writing and running pact (unit) tests
Where did you get this idea from? If in our docs, we should clarify as it is not expected that these are manually edited
s

Still

03/10/2022, 9:52 AM
Yes its not mentioned in the docs, but after running my unit test I don't see a json file being generated, so I wonder where I am going wrong
m

Matt (pactflow.io / pact-js / pact-go)

03/10/2022, 11:47 AM
So I’ve just cloned https://github.com/pact-foundation/pact-ruby cd
example/zoo-app
bundle install
rake spec
the pacts are generated:
Copy code
cat spec/pacts/zoo_app-animal_service.json
{
  "consumer": {
    "name": "Zoo App"
  },
  "provider": {
    "name": "Animal Service"
  },
  "interactions": [
    {
      "description": "a request for an alligator",
      "providerState": "there is an alligator named Mary",
      "request": {
        "method": "get",
        "path": "/alligators/Mary",
        "headers": {
          "Accept": "application/json"
        }
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json;charset=utf-8"
        },....
what happens if you do the same?
If I then try to verify it:
Copy code
➜  animal-service git:(master) ✗ rake pact:verify
SPEC_OPTS='' /Users/matthewfellows/.rvm/rubies/ruby-2.7.3/bin/ruby -S pact verify --pact-helper /Users/matthewfellows/development/public/pact-ruby/example/animal-service/spec/service_consumers/pact_helper.rb
INFO: Reading pact at ../zoo-app/spec/pacts/zoo_app-animal_service.json
WARN: Please note: we are tracking events anonymously to gather important usage statistics like Pact-Ruby version
                and operating system. To disable tracking, set the 'PACT_DO_NOT_TRACK' environment
                variable to 'true'.


Verifying a pact between Zoo App and Animal Service
  Given there is an alligator named Mary
    a request for an alligator
      with GET /alligators/Mary
        returns a response which
          has status code 200
          has a matching body
          includes headers
            "Content-Type" which equals "application/json;charset=utf-8"
  Given there is not an alligator named Mary
    a request for an alligator
      with GET /alligators/Mary
        returns a response which
          has status code 404
  Given an error occurs retrieving an alligator
    a request for an alligator
      with GET /alligators/Mary
        returns a response which
          has status code 500
          has a matching body
          includes headers
            "Content-Type" which equals "application/json;charset=utf-8"

3 interactions, 0 failures

➜  animal-service git:(master) ✗ rm ../zoo-app/spec/pacts/zoo_app-animal_service.json
➜  animal-service git:(master) ✗ rake pact:verify
SPEC_OPTS='' /Users/matthewfellows/.rvm/rubies/ruby-2.7.3/bin/ruby -S pact verify --pact-helper /Users/matthewfellows/development/public/pact-ruby/example/animal-service/spec/service_consumers/pact_helper.rb
Error reading file from ../zoo-app/spec/pacts/zoo_app-animal_service.json
No such file or directory @ rb_sysopen - ../zoo-app/spec/pacts/zoo_app-animal_service.json /Users/matthewfellows/.rvm/gems/ruby-2.7.3/gems/pact-support-1.17.0/lib/pact/consumer_contract/pact_file.rb:53:in `read'
/Users/matthewfellows/.rvm/gems/ruby-2.7.3/gems/pact-support-1.17.0/lib/pact/consumer_contract/pact_file.rb:53:in `get_local'
/Users/matthewfellows/.rvm/gems/ruby-2.7.3/gems/pact-support-1.17.0/lib/pact/consumer_contract/pact_file.rb:43:in `render_pact'
/Users/matthewfellows/.rvm/gems/ruby-2.7.3/gems/pact-support-1.17.0/lib/pact/consumer_contract/pact_file.rb:24:in `read'
/Users/matthewfellows/development/public/pact-ruby/lib/pact/provider/pact_source.rb:17:in `pact_json'
/Users/matthewfellows/development/public/pact-ruby/lib/pact/provider/pact_spec_runner.rb:124:in `collect'
Given that these projects work, I would 1. Check if they work for you 2. If the do, find out what’s different about your setup 3. Create a reproducable example that somebody can run to help guide you (see howtorepro for more on that) I’m guessing that perhaps Ruby is not a language you use very often? (I don’t use it/know it well) If that’s the case, i’d strongly suggest you pair with somebody that does.
s

Still

03/10/2022, 2:08 PM
When I run rake spec I get error
rake pact:verify gives error
m

Matt (pactflow.io / pact-js / pact-go)

03/10/2022, 10:13 PM
You might be best prefixing them with
bundle exec …
I can’t say for sure, but looks like you’re using Ruby 3 there - maybe it’s not supported? (I’m not sure)
have you tried 2.x.x?
s

Still

03/11/2022, 8:21 AM
Yes using ruby 3.1.0
2 Views