2

バックエンドに ajax 呼び出しを行うコントローラーをテストしたいと考えています。そのため、ジャスミンとシノンを使用したいと思います。sinon でバックエンド サーバーを偽造するために、次のようなことを試しました。

describe("fake server", function() {
 var server;

 beforeEach(function() {
    this.server = sinon.fakeServer.create();
  });

  afterEach(function() {
    this.server.restore();
  });

 it("calls callback with deserialized data", function () {
    var callback = sinon.spy();

    this.server.respondWith("GET", "/comments/1",
    [200, {"Content-Type": "application/json"},
      '{"comment":{"id":1,"title":"ducks and ducks"}}']);

    commentController = App.CommentController.create();

    //commentController.bind('getComment', callback);

    commentController.getComment(); 

    this.server.respond(); 

    expect(callback.called).toBeTruthy();
    expect(callback.getCall(0).args[0].attributes)
      .toEqual({
        id: "1",
        title: "ducks and ducks"
      });
  });
});

私のコントローラーは次のようになります。

 App.CommentController = Ember.Controller.extend({
      getComment: function() {
        $.ajax({
          url: 'http://myapi/comments/' + id,
          //...
          error: function(jqXHR, textStatus){
            this.set("error",true);
            //do something
          },
          success: function(data) {
            this.set("error",false);
            //do something else
          }
        });
      }
  });

誰かがこれを実行する方法を教えてもらえますか?

4

1 に答える 1

0
this.server.respondWith("GET", "http://myapi.com/comments/1",
[200, {"Content-Type": "application/json"},
  '{"comment":{"id":1,"title":"ducks and ducks"}}']);

私の場合、URLは絶対でなければなりません。

于 2013-01-14T16:30:25.017 に答える