简体   繁体   中英

emberjs ember-data and jasmine testing

I am trying to test a DS.Model using jasmine. I am trying to test that when i create a record it is setup properly.

When i do

bb = App.BuildingBlock.createRecord(name: "Test")

i get the error

Cannot read property 'createRecord' of undefined

but if i do

bb = App.BuildingBlock.create(name: "Test")

i get

Error: You should not call create on a model. Instead, call createRecord with the attributes you would like to set.

So it seems to know its a model ??? Also if i do

expect(App.BuildingBlock).toBeDefined()

it passes.

Anyone help me with this ?

thanks Rick

Add the following to the top of your spec

store = DS.Store.create({ revision: 4 })

Then use

store.createRecord(App.BuildingBlock, {name: "Test"});

The reason for the errors you're seeing is that DS.Model aliases createRecord to the store and that is what is undefined.

At the bottom of model.js you will see the following lines:

DS.Model.reopenClass({
  isLoaded: storeAlias('recordIsLoaded'),
  find: storeAlias('find'),
  filter: storeAlias('filter'),

  _create: DS.Model.create,

  create: function() {
    throw new Ember.Error("You should not call `create` on a model. Instead, call `createRecord` with the attributes you would like to set.");
  },

  createRecord: storeAlias('createRecord')
});

Can you try creating a JsFiddle of your problem? I don't see any problem with your first call to create the DS Model. You can't call .create() on a DS Model so that makes sense and your DS Model seems to exist. Are these tests being run with two different setups where the call to createRecord App or BuildingBlock don't exist? Are you sure your store is defined on your App (App.store) and that there is something wrong there?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM