单元测试中的序列化程序不处理json

时间:2015-07-11 04:24:04

标签: unit-testing ember.js ember-data

单元测试中的序列化器根本不处理json,但它在应用程序中按预期工作。是的,之后我写了它,但问题是 - 为什么它不起作用?我也尝试在适当的位置创建它,从RESTSerializer继承,创建模型,但没有一个工作。

也许有人可以提供线索?

更新 看起来一切都始于

 isPrimaryType: function (store, typeName, primaryTypeClass) {
    var typeClass = store.modelFor(typeName);
    return typeClass.modelName === primaryTypeClass.modelName;
  },

last string返回false,因为primaryTypeClass.modelName是undefined

序列化程序单元测试

import DS from 'ember-data';
import { moduleForModel, test } from 'ember-qunit';
import setupStore from 'app/tests/helpers/setup-store';
import Profile from 'app/models/profile';
import Email from 'app/models/email';
import Address from 'app/models/address';
import ProfileSerializer from 'app/serializers/profile';


var env;

moduleForModel('profile', 'Unit | Serializer | profile', {
  needs: ['serializer:profile', 'serializer:email', 'serializer:address', 'model:contactable', 'model:email', 'model:address'],

  beforeEach: function () {
    env = setupStore({
      profile: Profile,
      email: Email,
      address: Address
    });
    env.registry.register('serializer:profile', ProfileSerializer);
    env.profileSerializer = env.container.lookup('serializer:profile');
  },
   teardown: function() {
    Ember.run(env.store, 'destroy');
  }
});

test('it converts embedded records attributes', function(assert) {
  // expect(3);
  let payload = {
    id: 1,
    first_name: "Carlo",
    last_name: "Schuppe",
    company: "Metz-Witting",
    birthday: "01-10-1985",
    photo: null,
    emails: [{address: "foo@bar.baz", id: 1, type: "main"}],
    addresses: [{city: "Brooklyn", id: 1,  type: "main"}]
  },
  parsed = {
    "data":
    {
      "id":"1",
      "type":"profile",
      "attributes": { "firstName":"Carlo","lastName":"Schuppe","company":"Metz-Witting","birthday":"01-10-1985","photo":null },
      "relationships": {
        "emails": { "data": [{"id":"1","type":"email"}] },
        "addresses": { "data": [{"id":"1","type":"address"}] }
      }
    },
    "included":[
      {"id":"1","type":"email","attributes":{"address":"foo@bar.baz", "kind": "main"},"relationships":{"contactable":{"data":{"type":"profile","id":"1"}}}},
      {"id":"1","type":"address","attributes":{"city":"Brooklyn", "kind": "main"},"relationships":{"contactable":{"data":{"type":"profile","id":"1"}}}}
    ]
  },
  find, update, findAllRecordsJSON;

  Ember.run(function() {
    find = env.profileSerializer.normalizeResponse(env.store, Profile, payload, '1', 'findRecord');
    // update = env.profileSerializer.normalizeResponse(env.store, Profile, payload, '1', 'updateRecord');
    // findAllRecordsJSON = env.profileSerializer.normalizeResponse(env.store, Profile, payload, '1', 'findAll');
  });

  assert.deepEqual(find, parsed);
  // assert.deepEqual(update, parsed);
  // assert.deepEqual(findAllRecordsJSON, parsed);
});

setup_store.js

import Ember from 'ember';
import DS from 'ember-data';
// import ActiveModelAdapter from 'active-model-adapter';
// import ActiveModelSerializer from 'active-model-adapter/active-model-serializer';

export default function setupStore(options) {
  var container, registry;
  var env = {};
  options = options || {};

  if (Ember.Registry) {
    registry = env.registry = new Ember.Registry();
    container = env.container = registry.container();
  } else {
    container = env.container = new Ember.Container();
    registry = env.registry = container;
  }

  env.replaceContainerNormalize = function replaceContainerNormalize(fn) {
    if (env.registry) {
      env.registry.normalize = fn;
    } else {
      env.container.normalize = fn;
    }
  };

  var adapter = env.adapter = (options.adapter || '-default');
  delete options.adapter;

  if (typeof adapter !== 'string') {
    env.registry.register('adapter:-ember-data-test-custom', adapter);
    adapter = '-ember-data-test-custom';
  }

  for (var prop in options) {
    registry.register('model:' + Ember.String.dasherize(prop), options[prop]);
  }

  registry.register('store:main', DS.Store.extend({
    adapter: adapter
  }));

  registry.optionsForType('serializer', { singleton: false });
  registry.optionsForType('adapter', { singleton: false });
  registry.register('adapter:-default', DS.Adapter);

  registry.register('serializer:-default', DS.JSONSerializer);
  registry.register('serializer:-rest', DS.RESTSerializer);
  registry.register('serializer:-rest-new', DS.RESTSerializer.extend({ isNewSerializerAPI: true }));

  registry.register('adapter:-active-model', DS.ActiveModelAdapter);
  registry.register('serializer:-active-model', DS.ActiveModelSerializer.extend({isNewSerializerAPI: true}));

  registry.register('adapter:-rest', DS.RESTAdapter);

  registry.injection('serializer', 'store', 'store:main');
  registry.register('transform:string', DS.StringTransform);
  registry.register('transform:number', DS.NumberTransform);
  registry.register('transform:date', DS.DateTransform);
  registry.register('transform:main', DS.Transform);

  env.serializer = container.lookup('serializer:-default');
  env.restSerializer = container.lookup('serializer:-rest');
  env.restNewSerializer = container.lookup('serializer:-rest-new');
  env.store = container.lookup('store:main');
  env.adapter = env.store.get('defaultAdapter');

  env.registry.register('serializer:-active-model', DS.ActiveModelSerializer.extend({isNewSerializerAPI: true}));
  env.registry.register('adapter:-active-model', DS.ActiveModelAdapter);
  env.registry.register('serializer:application', DS.ActiveModelSerializer.extend({isNewSerializerAPI: true}));


  return env;
}

输出

{
  "data": null,
  "included": []
}

0 个答案:

没有答案