pact-mock_service icon indicating copy to clipboard operation
pact-mock_service copied to clipboard

Verifying before Received, resulting in missing request error.

Open casertap opened this issue 9 years ago • 10 comments

My tests are working with chrome but failing with PhantomJs It looks like the verification is done before the request with phantomjs.

The output I get from the pact-mock_service is:

I, [2016-05-16T15:41:49.828460 #79772]  INFO -- : Registered expected interaction POST /api/v1/campaigns
D, [2016-05-16T15:41:49.828915 #79772] DEBUG -- : {
  "description": "a campaign will be created",
  "provider_state": "we submit a new campaign",
  "request": {
    "method": "post",
    "path": "/api/v1/campaigns",
...
W, [2016-05-16T15:41:49.928726 #79772]  WARN -- : Verifying - actual interactions do not match expected interactions for example "".
Missing requests:
        POST /api/v1/campaigns


W, [2016-05-16T15:41:49.928768 #79772]  WARN -- : Missing requests:
        POST /api/v1/campaigns


I, [2016-05-16T15:41:49.930866 #79772]  INFO -- : Received request POST /api/v1/campaigns
D, [2016-05-16T15:41:49.931029 #79772] DEBUG -- : {

I, [2016-05-16T16:26:14.649965 #81885]  INFO -- : Found matching response for POST /api/v1/campaigns

on my test console:

✔ 0 tests completed
✖ 1 test failed

FAILED TESTS:
  PactCampaignCreate
    handleSubmit
      ✖ should send the correct request
        PhantomJS 2.1.1 (Mac OS X 0.0.0)
      AssertionError: expected [Error:
      pact-consumer-js-dsl: Pact verification failed
      Actual interactions do not match expected interactions for mock MockService.

      Missing requests:
        POST /api/v1/campaigns

      See standard out/err for details.
      ] to equal null (/Users/pierrecaserta/workspace/jupiter-admin/tests.webpack.js:24570 <- webpack:///~/chai/lib/chai/assertion.js:111:0)

Here is my test code:

  let pactMockService = null;
  let renderer = null;
  let form = null;

  beforeEach(() => {
    pactMockService = Pact.mockService({
      consumer: 'campaign create consumer',
      provider: 'campaign create provider',
      port: 1234,
      done: (error) => {
        expect(error).to.equal(null);
      }
    });
    // pactMockService.resetSession(done);
  });

  describe('handleSubmit', () => {
    beforeEach( () => {

      const spy = chai.spy((d) => {
        data = preprocessBeforeSend(d);
      });
      renderer = rendererValidation(createStore(browserHistory, client, mockStore), spy);
      form = ReactTestUtils.findRenderedDOMComponentWithClass(renderer, "form-horizontal");
    });

    afterEach( () => {
      ReactDOM.unmountComponentAtNode(ReactDOM.findDOMNode(renderer).parentNode);
    });

    it("should send the correct request", (done) => {

      const responseBody = {"id": Pact.Match.somethingLike(1097),
            "name":"492699",
            "start_date":"2016-06-04T00:00:00Z",
            "end_date":"2016-07-01T00:00:00Z",
            "internal_code": Pact.Match.somethingLike("8bbddbe518ed43368fc932a1092e0983"),
            "timezone":"Sydney",
            "cpc":0.5,
            "total_clicks":0,
            "max_clicks":186,
            "total_daily_clicks":0,
            "max_daily_clicks":76,
            "total_impressions":0,
            "budget":197.0,
            "advertiser_id":333555,
            "active":true,
            "priority_id":3,
            "frequency_capped":true,
            "freq_cap_time_unit":"day",
            "freq_cap_period":10,
            "freq_cap_count":5,
            "freq_cap_type":"impression",
            "created_at": Pact.Match.somethingLike("2016-05-09T04:36:33Z"),
            "updated_at":Pact.Match.somethingLike("2016-05-09T04:36:33Z"),
            "deleted_at":null,
            "disable_daily_cap":false,
            "target_platform":"both",
            "target_category_ids":[6263, 6076, 6317],
            "target_keywords":null,
            "target_sites":null,
            "target_location_ids":[6047981],
            "target_subcategory_ids":[]
      };

      pactMockService
      .given("we submit a new campaign")
      .uponReceiving("a campaign will be created")
      .withRequest({
        method: "post",
        path: "/api/v1/campaigns"
      }).willRespondWith({
        status: 201,
        headers: {"Content-Type": "application/json; charset=utf-8"},
        body: responseBody
      });
      pactMockService.run(done, (runComplete) => {
        ReactTestUtils.Simulate.submit(form);
        const headers = { "Accept": "*/*",
          "Accept-Encoding":"gzip, deflate",
          "Accept-Language":"en-US,en;q=0.8",
          "Connection":"keep-alive",
          "Content-Length":"491",
          "Content-Type":"application/json",
          "Host":"127.0.0.1:1234",
          "X-APIKEY": "49b4102ec4aad5a025fd906b4867bf11" };
        client.post('campaigns', {data: data, headers: headers}).then((r) => {
          // I do not care about the fake response
          expect(r.body.name).to.equal(responseBody.name);
        }).catch((e) => {
          console.log(e);
        });
        runComplete();
      });
    });

  });

casertap avatar May 16 '16 06:05 casertap

It looks like you do runComplete() before your client.post-promise resolves.

bramkoot avatar Jan 14 '17 15:01 bramkoot

Is this still an issue?

bethesque avatar Apr 07 '17 04:04 bethesque

I'm running into similar issues. Has there been any resolution?

rrakin avatar Nov 23 '17 03:11 rrakin

@mefellows or @mboudreau may have an idea. Please create a runnable example to demonstrate your issue - you could start by cloning the pact-js repo and modifying one of the examples.

bethesque avatar Nov 23 '17 04:11 bethesque

@rrakin we generally see this when promises aren't properly handled (e.g. a before all exists before promise completes) or when there is a logic error (e.g. before all/after all sweeping away interactions between tests.

If you could share a repro that would help.

mefellows avatar Nov 23 '17 07:11 mefellows

@mefellows here is a sample gist i'm trying to use. https://gist.github.com/anonymous/dca87a177e88503efe01155c04747cfe. Please let me know if this helps.

rrakin avatar Nov 23 '17 13:11 rrakin

Thanks @rrakin.

Please see the note on jest, i'd say the issue is that you have your verification step in a separate it block, and they are run non-deterministically - in parallel.

If you add the --runInBand flag that should probably sort out your issue.

mefellows avatar Nov 23 '17 19:11 mefellows

@mefellows, thanks for the feedback.

I tried this before:

it("returns data", done => {
   return getData().then(response => {
     expect(response).toEqual(expectedResult);
     done();
   });
   provider.verify(); 
 });

However, the pact isn't being published in the pact folder.

rrakin avatar Nov 23 '17 20:11 rrakin

It won't be written until finalize is called.

bethesque avatar Nov 23 '17 20:11 bethesque

That return for the promise will never hit your verify unless you put it within the then.

On Fri., 24 Nov. 2017, 7:38 am Beth Skurrie, [email protected] wrote:

It won't be written until finalize is called.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/pact-foundation/pact-mock_service/issues/49#issuecomment-346694605, or mute the thread https://github.com/notifications/unsubscribe-auth/AAjA5JfACXKXBa5tVm8zJ8HfrPo3BSd-ks5s5dfDgaJpZM4IfEnE .

mboudreau avatar Nov 23 '17 20:11 mboudreau

Closing - if anyone receives similar errors, please raise as a new issue. Looks like this was down to code configuration, and pointed out above 👍🏾

YOU54F avatar Jun 26 '24 00:06 YOU54F