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

Getting error from rspec-mocks while trying to stub a method call #237

Closed m-resende closed 2 months ago

m-resende commented 3 years ago

Hello!

I'm trying to stub a method call when verifying contracts, but I'm currently getting the following error:

The use of doubles or partial doubles from rspec-mocks outside of the per-test lifecycle is not supported

I have the provider setup like this:

Pact.provider_states_for 'my provider' do
  provider_state 'a state exists' do
    set_up do
      allow(MyModule::MyClass).to receive(:my_method).and_return({ bla: 1})
    end

    tear_down do
    end
  end
end

My pact_helper.rb:

require 'pact/provider/rspec'
require 'spec_helper'

Pact.service_provider 'provider' do
  honours_pacts_from_pact_broker do
    pact_broker_base_url 'http://localhost:9292/'
    consumer_version_tags ['local']
    app_version '1234'
    publish_verification_results true
  end
end

I found this example while looking for testing error responses

Do I need to configure something else in order for this to work?

m-resende commented 3 years ago

For future reference, you need to add this to your pact_helper.rb:

Pact.configure do |config|
  config.include RSpec::Mocks::ExampleMethods
end
m-resende commented 3 years ago

Actually, I'm still getting the same error even after adding the Rspec::Mock::ExampleMethods would anyone know more about it?

I'm trying to stub a method call that creates the user and authenticates them. If there's another solution, I'd be happy to try

bethesque commented 3 years ago

I think something in RSpec has changed since I wrote that example. I'm not sure if we'll be able to work around it or not, but I'll have a little dig for you, just in case. Would you please follow these instructions to get a reproduction of your issue for me in code. https://github.com/pact-foundation/pact-ruby-e2e-example

YOU54F commented 2 months ago

If anyone is still having this issue, please create a reproducible example!

Thank you