pact-foundation/pact-mock_service

NoMethodError - undefined method `[]' for nil:NilClass

cwilliamson226 opened this issue · 3 comments

Hi, I'm trying to set up a mock service to get called from our front-end protractor tests in order to transition to contract testing. I spent a while trying to integrate pact-js with our testing structure, but it ended up being more trouble than it's worth, so I decided to try using pact-mock-service instead.

I successfully set up some calls to the service using DELETE /interactions to clear previous interactions and a GET call to hit the service, but when I try to use POST /interactions to send an interaction to add, I get the error Error ocurred in mock service: NoMethodError - undefined method `[]' for nil:NilClass.

I tried using Postman to hit the mock-service once I ran it, and it turns out that if I send the same POST call from there, it successfully registers the interaction. After doing that, I'm able to send the GET request and it works exactly how it should. However, when I try sending the POST request from my test file, it throws the NoMethodError. I've honestly never used Ruby, so I don't even know how I could start trying to debug the error.

I made a gist with the sample code that I have that throws the error on use here: https://gist.github.com/cwilliamson226/60dc289807658278aedac310dc56a5cd

Here is the command that I'm using to run the mock-service:
pact-mock-service --consumer Foo --provider Bar --port 4446 --log ./logs/pacts.log --cors=CORS

And lastly here is the full error log that I get when I try to make the POST call:
E, [2018-06-06T17:20:54.861657 #22411] ERROR -- : Error ocurred in mock service: NoMethodError - undefined method `[]' for nil:NilClass E, [2018-06-06T17:20:54.861794 #22411] ERROR -- : /Users/cwilliamson226/.rvm/gems/ruby-2.4.2@global/gems/pact-support-1.6.2/lib/pact/consumer_contract/interaction.rb:32:in `parse_v2_interaction' /Users/cwilliamson226/.rvm/gems/ruby-2.4.2@global/gems/pact-support-1.6.2/lib/pact/consumer_contract/interaction.rb:26:in `from_hash' /Users/cwilliamson226/.rvm/gems/ruby-2.4.2@global/gems/pact-mock_service-2.8.1/lib/pact/mock_service/request_handlers/interaction_post.rb:24:in `respond' /Users/cwilliamson226/.rvm/gems/ruby-2.4.2@global/gems/pact-mock_service-2.8.1/lib/pact/mock_service/request_handlers/base_request_handler.rb:17:in `call' /Users/cwilliamson226/.rvm/gems/ruby-2.4.2@global/gems/rack-2.0.5/lib/rack/cascade.rb:33:in `block in call' /Users/cwilliamson226/.rvm/gems/ruby-2.4.2@global/gems/rack-2.0.5/lib/rack/cascade.rb:24:in `each' /Users/cwilliamson226/.rvm/gems/ruby-2.4.2@global/gems/rack-2.0.5/lib/rack/cascade.rb:24:in `call' /Users/cwilliamson226/.rvm/gems/ruby-2.4.2@global/gems/pact-mock_service-2.8.1/lib/pact/consumer/mock_service/cors_origin_header_middleware.rb:11:in `call' /Users/cwilliamson226/.rvm/gems/ruby-2.4.2@global/gems/pact-mock_service-2.8.1/lib/pact/consumer/mock_service/error_handler.rb:13:in `call' /Users/cwilliamson226/.rvm/gems/ruby-2.4.2@global/gems/pact-mock_service-2.8.1/lib/pact/mock_service/app.rb:33:in `call' /Users/cwilliamson226/.rvm/gems/ruby-2.4.2@global/gems/pact-mock_service-2.8.1/lib/pact/consumer/mock_service/set_location.rb:14:in `call' /Users/cwilliamson226/.rvm/gems/ruby-2.4.2@global/gems/rack-2.0.5/lib/rack/handler/webrick.rb:86:in `service' /Users/cwilliamson226/.rvm/gems/ruby-2.4.2@global/gems/webrick-1.4.2/lib/webrick/httpserver.rb:140:in `service' /Users/cwilliamson226/.rvm/gems/ruby-2.4.2@global/gems/webrick-1.4.2/lib/webrick/httpserver.rb:96:in `run' /Users/cwilliamson226/.rvm/gems/ruby-2.4.2@global/gems/webrick-1.4.2/lib/webrick/server.rb:307:in `block in start_thread'

Any help would be greatly appreciated!

Ruby isn't that different to javascript! If you look at the line, you'll be able to have a fair guess why the exception is being thrown (why the data is like that in the first place is another question).

def self.parse_v2_interaction hash, options
        request_hash = Pact::MatchingRules.merge(hash['request'], hash['request']['matchingRules'], options)

So, to me, it looks like hash is nil. Which is pretty weird.

Can you see if you can recreate the issue using this example codebase? https://github.com/pact-foundation/pact-ruby-standalone-e2e-example

I'll be able to help you much more easily that way.

I wasn't able to really set up a good example of what I was doing using that codebase, so I unfortunately wasn't able to pinpoint the issue I was having. I ended up just using a bash script to set up all of my interactions and mock server and then just send calls from my Protractor test files.

This seems to be working the way that I wanted, as now I just immediately register my interactions from the same script that runs the mock-server.

Thanks for the help though!

Glad you got it working. I'll close this now then.