salsify / ember-cli-pact

Contract testing with Ember.js and Pact
MIT License
42 stars 13 forks source link

Cannot create a DELETE request #32

Open louiseavelar opened 5 years ago

louiseavelar commented 5 years ago

Hi,

We are trying to create a delete request using ember-pact-cli and mirage using the following steps: ` let model = server.create('model', { name: 'some model' });

given('a model item exists', model);

let actualModel = await this.store().findRecord('model', 1);

actualModel.deleteRecord();

await interaction(function() { actualModel.save(); });`

OR

` let model = server.create('model', { name: 'some model' });

given('a model item exists', model);

let actualModel = await this.store().findRecord('model', 1);

await interaction(function() { actualModel.destroyRecord(); });`

Everytime I run the ember tests to generate the pact file, I am getting the following error: cannot read property 'method' of null

I've cloned this repo and added a delete test on the examples here and the same error is being thrown as well.

I was wondering if this is the correct way of creating a DELETE request or wether this could be a bug or not.

Thanks in advance.

louiseavelar commented 5 years ago

Can anyone help please?

springerigor commented 5 years ago

Hey @louiseavelar. I encountered the very same problem and managed to workaround it by explicitly calling getProvider().interaction.recordRequest(request);

Full working test:

import { test, module } from 'qunit';
import { setupTest } from 'ember-qunit';
import { getProvider, given, interaction, setupPact } from 'ember-cli-pact';

module('Pact | Users', function (hooks) {
  setupTest(hooks);
  setupPact(hooks);

  test('requesting current user', async function (assert) {
    given('the user is not signed in');

    getProvider().map((server) => {
      return server.get('/my/url', (request) => {
        const responseHeaders = { 'Content-Type': 'application/json' };

        // Force to record interaction, otherwise Pact is not generated.
        getProvider().interaction.recordRequest(request);

        return [401, responseHeaders, ''];
      });
    });

    try {
      await interaction(() => this.store().findRecord('user', 'me'));
    } catch (err) {
      assert.equal(err.errors[0].status, 401);
    }
  });
});
gustavocabral commented 4 years ago

@louiseavelar, the reason ember-cli-pact fails like this is because the provider, in your case mirage, will not call the serializer when it respondes with an empty response:

https://github.com/miragejs/ember-cli-mirage/blob/v1.0.0/addon/serializer-registry.js#L47

Ember-cli-pact relies on a serializer mixin to intercept the provides' response, such as in this test:

https://github.com/salsify/ember-cli-pact/blob/af855a98b1351b291c75a2619b98463139c70db9/tests/dummy/mirage/serializers/application.js#L4

As @springerigor replied, getting the Provider and recording the request is a workaround. It will records the request on the provider's side, even before the response is serialized. But that is still a workaround. 😅