Generated unit test for application serializer fails


#1

I generated application serializer in a vanilla ember-cli 2.4.3 project:

    ember g serializer application

but now the generated test tests/unit/serializers/application-test.js is failing with message

    Promise rejected before it serializes records: Attempting to register an unknown factory: `model:application`

It found this ember-cli issue that pointed me to this issue but it is closed without any hint on how to fix the failing test.

From the above mentioned issues, I get that the test is looking for application model that does not exist. The test is to make sure that the serialize() function works fine.

Can any one comment on how to get this working, please?


#2

I’m having problems with it too, same exact one, please let me know if you fix it


#3

Which version of Ember Data are you using? It’s possible the referenced commit landed after the Ember Data version you are using was published …


#4

I changed the test to:

import { moduleFor, test } from 'ember-qunit';
import Ember from 'ember';
import DS from 'ember-data';


moduleFor('serializer:application', 'Unit | Serializer | application', {
  // Specify the other units that are required for this test.
});

test('it serializes records in JSON Api format', function(assert) {

  // create a dummy model for application
  let DummyModel = DS.Model.extend({
    name: DS.attr('string'),
    address: DS.attr('string')
  });
  this.registry.register('model:application', DummyModel);

  let store = Ember.getOwner(this).lookup('service:store');

  let basicModel = {
    name: 'Test Name',
    address: 'SOme Dummy Address'
  };

  let expectedHash = {
    data: {
      attributes: {
        name: basicModel.name,
        address: basicModel.address
      },
      type: 'applications'
    }
  };

  Ember.run(function(){

    // Create an instance of DummyModel and serialize
    let serializedRecord = store.createRecord('application', basicModel).serialize();
    assert.deepEqual(serializedRecord, expectedHash);

  });

});

#5

I’m using ember-data 2.5.3


#6

solved my problem thx :wink:


#7

An ember@2.18 version of this, using getOwner, wait instead of run, and @ember namespaced modules. I would have gone with async/await but that requires configuration changes.

import { moduleFor, test } from 'ember-qunit';
import attr from 'ember-data/attr';
import Model from 'ember-data/model';
import wait from 'ember-test-helpers/wait';
import { getOwner } from '@ember/application';

moduleFor('serializer:application', 'Unit | Serializer | application', {
});

test('it serializes records in JSON Api format', function(assert) {
    const owner = getOwner(this);
    const store = owner.lookup('service:store');

    // create a dummy model for application
    let DummyModel = Model.extend({
        name: attr('string'),
        address: attr('string')
    });
    owner.register('model:application', DummyModel);

    let basicModel = {
        name: 'Test Name',
        address: 'SOme Dummy Address'
    };

    let expectedHash = {
        data: {
            attributes: {
                name: basicModel.name,
                address: basicModel.address
            },
            type: 'applications'
        }
    };

    return wait().then(() => {
        // Create an instance of DummyModel and serialize
        let serializedRecord = store.createRecord('application', basicModel).serialize();
        assert.deepEqual(serializedRecord, expectedHash);
    });
});