sinon.jsとjasmine.jsを使用して、最初のbackbone.jsアプリケーションの単体テストを試みています。
この特定のテストケースでは、sinon.js fakeServerメソッドを使用して、次の構造のダミー応答を返しました。
beforeEach( function(){
this.fixtures = {
Tasks:{
valid:{
"tasks":[
{
id: 4,
name:'Need to complete tests',
status: 0
},
{
id: 2,
name:'Need to complete tests',
status: 1
},
{
id: 3,
name:'Need to complete tests',
status: 2,
}
]
}
}
};
});
したがって、以下のテストケースで実際にfetch呼び出しを呼び出すと、3つのモデルが正しく返されます。コレクションのparseメソッドで、ルートの「tasks」キーを削除し、backbone.jsのドキュメントに記載されているオブジェクトの配列のみを返すようにしました。しかし、これを行うと、モデルはコレクションに追加されず、collection.lengthは0を返します。
describe("it should make the correct request", function(){
beforeEach( function(){
this.server = sinon.fakeServer.create();
this.tasks = new T.Tasks();
this.server.respondWith('GET','/tasks', this.validResponse( this.fixtures.Tasks.valid) );
});
it("should add the models to the tasks collections", function(){
this.tasks.fetch();
this.server.respond();
expect( this.tasks.length ).toEqual( this.fixtures.Tasks.valid.tasks.length );
});
afterEach(function() {
this.server.restore();
});
});
タスクコレクション
T.Tasks = Backbone.Collection.extend({
model: T.Task,
url:"/tasks",
parse: function( resp, xhr ){
return resp["tasks"];
}
});
ここで何が間違っているのか教えていただけますか?