1

我有一个 ember.js 应用程序,我正在设置DS.Store这样的应用程序(查看实际代码):

(function (app) {
    'use strict';

    ...

    var store = DS.Store.extend({
        revision: 12
    });

    app.Store = store;

})(window.Balanced);

现在我有一个 qunit 测试,在那个测试中,我想将默认的 RESTAdapter 换成 FixtureAdapter,这样我就可以为我的模型设置夹具。我想我需要写这样的东西,但我不是 100% 确定:

(function () {
    'use strict';

    var fixtureAdapter;

    module('tests.store.module', {
            setup: function () {
                fixtureAdapter = DS.FixtureAdapter.extend({

                });
                Balanced.Store.reopen({
                    adapter: fixtureAdapter
                });

                //  TODO: how does this work?
                Balanced.Marketplace.FIXTURES = [
                    {id: 1, name: '1'},
                    {id: 2, name: 'poop'},
                    {id: 3, name: 'poop'}
                ];
            },
            teardown: function () {
                // teardown code
            }
        }
    );

    test("Marketplace query", function () {
        var marketplaces = Balanced.Marketplace.find();
        //  TODO: how do I test this?
    });
})();
4

1 回答 1

2

对于我使用 jasmine 进行的基本单元测试,我像这样手动设置存储(使用本地存储适配器来避免 xhr 请求)

describe ("CodeCamp.SessionView Tests", function(){

  var get = Ember.get, set = Ember.set, sut, controller, session, store;

  beforeEach(function(){
    store = DS.Store.create({
      revision: 11,
      adapter: DS.LSAdapter.create()
    });
    sut = CodeCamp.SessionView.create();
    controller = CodeCamp.SessionController.create();
    controller.set("store", store);
    sut.set("controller", controller);
    session = CodeCamp.Session.createRecord({ id: 1, name: "First", room: "A", ratings: [], speakers: [], tags: []});
  });

  afterEach(function() {
    Ember.run(function() {
      store.destroy();
      controller.destroy();
      sut.destroy();
      session.destroy();
    });
    store = null;
    controller = null;
    sut = null;
    session = null;
  });

  it ("will create rating when form is valid", function(){
    sut.set('score', '1234');
    sut.set('feedback', 'abcd');
    sut.addRating(session);
    var ratings = CodeCamp.Session.find(1).get('ratings');
    var rating = ratings.objectAt(0);
    expect(rating.get('score')).toEqual('1234');
    expect(rating.get('feedback')).toEqual('abcd');
    expect(rating.get('session').get('id')).toEqual(1);
  });

});

上面的测试针对以下 ember 视图进行端到端的测试

CodeCamp.SessionView = Ember.View.extend({
  templateName: 'session',
  addRating: function(event) {
    if (this.formIsValid()) {
      var rating = this.buildRatingFromInputs(event);
      this.get('controller').addRating(rating);
      this.resetForm();
    }
  },
  buildRatingFromInputs: function(session) {
    var score = this.get('score');
    var feedback = this.get('feedback');
    return CodeCamp.Rating.createRecord(
    { score: score,
      feedback: feedback,
      session: session
    });
  },
  formIsValid: function() {
    var score = this.get('score');
    var feedback = this.get('feedback');
    if (score === undefined || feedback === undefined || score.trim() === "" || feedback.trim() === "") {
      return false;
    }
    return true;
  },
  resetForm: function() {
    this.set('score', '');
    this.set('feedback', '');
  }
});

如果您想查看整个应用程序的运行情况(只是一个带有一些基本 jasmine 测试的示例 ember 应用程序),它位于 github

https://github.com/toranb/ember-code-camp/

于 2013-03-21T12:23:56.217 回答