質問

I have a simple ember application and I want to test it, without making server for API calls.

I've looked around a bunch and found this piece of code which helps tremendously. Problem is for testing I want to use the fixture adapter (makes sense, right?)

 @store = Lfg.__container__.lookup('store:main')

Here is my model:

Lfg.Activity = DS.Model.extend
  title: DS.attr('string')
  people: DS.attr('number')
  maxPeople: DS.attr('number')
  host: DS.attr('string')

Then inside an Em.run => I do this

  Lfg.reset()

  container = new Ember.Container()
  # These are my models... just one for now.
  [
    'activity'
  ].forEach (x,i) ->
    container.register 'model:'+x, Lfg.get( Ember.String.classify(x) )
  # It's essentially just: container.register("model:activity", Lfg.Activity)


  @store = DS.Store.create
    adapter: DS.FixtureAdapter.extend()
    container: container

But I keep getting errors with the serializer. I tried adding the serializer but doesn't help. Do I need to container.register other things as well?

The error I get is TypeError: Cannot call method 'serialize' of undefined coming from mockJSON method, more specifically store.serializerFor(type) is returning null.

If I set store via store = Lfg.__container__.lookup('store:main') and then store.serializerFor(Lfg.Activity) it seems to work ok in the console -- is this not the same store? I want to use the one with the fixture adapter. I tried setting the serializer but that didn't help.

役に立ちましたか?

解決

I prefer using something like mockjax to mock the api endpoints, then using qunit and the built in helpers provided by Ember and qunit

Here's an example of how to set up a simple json response

$.mockjax({
    url:  '/colors',
    dataType: 'json',
  responseText: {
    colors:[
      {
        id: 1,
        color: "red"
      },
      {
        id: 2,
        color: "green"
      },
      {
        id: 3,
        color: "blue"
      }
     ]
  }
});

And a test that would hit this endpoint

test("root lists 3 colors", function(){
  var store = App.__container__.lookup('store:main');
  var colors = store.find('color');
  stop();
  colors.then(function(arr){
    start();
    equal(arr.get('length'), 3, 'store returns 3 records');
  });      
});

http://emberjs.jsbin.com/wipo/3/edit

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top