In your consumer test class, instead of calling the “subject.methodToTest()”,
For example:
describe MethodToTest{
before do
AppleServiceClient.base_uri ‘localhost:1234’
end
subject { AppleServiceClient.new }
describe “get_an_apple” do
before do
apple_service.given(“an apple exists”).
upon_receiving(“a request for an apple”).
with(method: :get, path: ‘/apple’, query: ”).
will_respond_with(
status: 200,
headers: {‘Content-Type’ => ‘application/json’},
body: {name: ‘Red Apple’} )
end
it “returns a an apple” do
expect(subject.get_apple).to eq(Apple.new(‘Red Apple’))
end
}
We could change our app to point to the mock localhost.
For example, in Ruby on Rails, we could change config->environments->environment.rb for the url for AppleServiceclient to point to the mock service at http://localhost:1234. Or better way is to create a new environment config for the Pact test.
# AppleServiceAPI
config.appleServiceApi_url = “http://localhost:1234”
Therefore, the new code in the assertion section will look something like:
it “returns an apple” do
expect(GetApple.get_apple).to eq(Apple.new(‘Red Apple’))
end
where GetApple is the function in the application that makes the actual call to the AppleServiceClient.
Leave a Reply