Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handleFindAll() mockjax return always includes embedded reccords #134

Closed
Andrew-Max opened this issue Sep 22, 2015 · 12 comments
Closed

handleFindAll() mockjax return always includes embedded reccords #134

Andrew-Max opened this issue Sep 22, 2015 · 12 comments

Comments

@Andrew-Max
Copy link

I have two models: map and flight.

When I call:

TestHelper.handleFindAll('map', 3);

I get an error

Error: Assertion Failed: Ember Data expected a number or string to represent the record(s) in the flightrelationship instead it found an object. If this is a polymorphic relationship please specify atypekey. If this is an embedded relationship please include theDS.EmbeddedRecordsMixinand specify theflight property in your serializer's attrs object.

My Flight factory:

FactoryGuy.define('map', {
  sequences: {
    fileUrl: function(num) {
      return ( num + '/');
    }
  },

  default: {
    type: 'ortho',
    flight: FactoryGuy.belongsTo('flight'),
    fileUrl: FactoryGuy.generate('fileUrl'),
    layers: []
  }
});

My test:

test('can view routes when authenticated', function(assert) {
  assert.expect(1);
  TestHelper.handleFindAll('map', 3);
  authenticateSession();
  visit('/maps/');

  andThen(function() {
    assert.equal(currentPath(), 'maps.index');
  });
});

Because a flight object is embedded in the returned record instead of the id which the serializer is looking for.

If I create a custom serializer for map which specifies that flights should be embedded with DS.EmbeddedRecordsMixin, this error goes away but I can't do this because my backend does not embed that record so the app breaks.

As far as I can see I have two options:

  1. Create a test only serializer for flight which seems undesirable and I wouldn't even know how to do it.

  2. Prevent mockjax from embedding the flight record and ask factory-guy to create it seperately.

Are either of these feasible or am I missing something here?

@danielspaniel
Copy link
Collaborator

you puzzle me @Andrew-Max
you said you had two models ( map and site ) .. is there also a flight object?
huh?
Me confused.

Would be nice if you laid this out alittle better ( more detail ) .
As it is I kinda am just guessing as to what your talking about/ and need help on.

@Andrew-Max
Copy link
Author

Fixed! I am not a smart man ...

Site should have been flight

@danielspaniel
Copy link
Collaborator

whew .. was about to call my mommy and start crying .. thanks for saving me the embarrasment :)

@Andrew-Max
Copy link
Author

@danielspaniel Sorry I should have specified, I fixed the typo in the original issue but I did not actually resolve the issue.

One thing which I did find solves the issue is to change
flight: FactoryGuy.belongsTo('flight'),
to
flight: 1,

But that doesn't match up with the documentation.

Any thought on whether or not that makes sense?

@danielspaniel
Copy link
Collaborator

what version are you using of factory guy/ ember data??
what do you get when you do => FactoryGuy.build('map') // can you post it

@Andrew-Max
Copy link
Author

"ember-data": "1.13.6",
"ember-data-factory-guy": "1.13.10",

with: flight: FactoryGuy.belongsTo('flight')

I get:
Object {type: "ortho", fileUrl: "test-orthomaps/M", flight: Object, layers: Array[0]…}

with: flight: 1
I get:

Object {type: "ortho", fileUrl: "test-orthomaps/M", flight: 1, layers: Array[0]…}

Which seems to match up with what I would expect

@danielspaniel
Copy link
Collaborator

what do your models look like?

@Andrew-Max
Copy link
Author

map model:

export default DS.Model.extend({
  fileUrl: DS.attr('string'),
  type: DS.attr('string'),
  urlPrefix: DS.attr('string'),
  flight: DS.belongsTo('flight', {async: true}),
  mapLayers: DS.hasMany('map-layer', {async: true}),
  kmlLayers: DS.hasMany('kml-layer', {async: true}),

  displayUrl: function () {
    return this.get('urlPrefix') + this.get('fileUrl');
  }.property('fileUrl')
});

flight model:

export default DS.Model.extend({
  executeAt: DS.attr('string'),
  maps: DS.hasMany('map', {async: true}),
  site: DS.belongsTo('site', {async: true}),
  twoDimProductsUrl: DS.attr('string'),
  threeDimProductsUrl: DS.attr('string'),
  photosUrl: DS.attr('string')
});

@danielspaniel
Copy link
Collaborator

second one is flight .. I reckon. Looks fine.
Can you switch to latest factoryguy 2.0.3 . I think it works with ember-data 1.13 as well, and I think it might solve this problem

@Andrew-Max
Copy link
Author

Yes, I will give that a go and report back later today. I really appreciate all the help.

@Andrew-Max
Copy link
Author

Upgrading to 2.0.3 fixed this issue! Many thanks @danielspaniel

@danielspaniel
Copy link
Collaborator

hooray .. I am glad you back in business! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants