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);
  });

});

上記のテストは、次の残り火ビューに対してエンドツーエンドで行われます

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', '');
  }
});

このアプリ全体の動作を確認したい場合(いくつかの基本的なジャスミンテストを含むサンプルの残り火アプリ)、githubにあります

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

于 2013-03-21T12:23:56.217 に答える