Lor*_*ard 11 javascript backbone.js jasmine backbone-relational
假设我有两个简单的fixture文件,一个用于用户(1),另一个用于消息(2).
消息的骨干模型如下(3).
如果我加载"消息夹具",我想在消息模型中指定有关用户的相关信息.
使用jasmine测试套件在规范视图(4)中激活此目标的正确方法是什么?
有关详细信息,请参阅(4)中的注释.
(1)
// User Fixture
beforeEach(function () {
this.fixtures = _.extend(this.fixtures || {}, {
Users: {
valid: {
status: 'OK',
version: '1.0',
response: {
users: [
{
id: 1,
name: 'olivier'
},
{
id: 2,
name: 'pierre',
},
{
id: 3,
name: 'george'
}
]
}
}
}
});
});
Run Code Online (Sandbox Code Playgroud)
(2)
// Message Fixture
beforeEach(function () {
this.fixtures = _.extend(this.fixtures || {}, {
Messages: {
valid: {
status: 'OK',
version: '1.0',
response: {
messages: [
{
sender_id: 1,
recipient_id: 2,
id: 1,
message: "Est inventore aliquam ipsa"
},
{
sender_id: 3,
recipient_id: 2,
id: 2,
message: "Et omnis quo perspiciatis qui"
}
]
}
}
}
});
});
Run Code Online (Sandbox Code Playgroud)
(3)
// Message model
MessageModel = Backbone.RelationalModel.extend({
relations: [
{
type: Backbone.HasOne,
key: 'recipient_user',
keySource: 'recipient_id',
keyDestination: 'recipient_user',
relatedModel: UserModel
},
{
type: Backbone.HasOne,
key: 'sender_user',
keySource: 'sender_id',
keyDestination: 'sender_user',
relatedModel: UserModel
}
]
});
Run Code Online (Sandbox Code Playgroud)
(4)
// Spec View
describe('MyView Spec', function () {
describe('when fetching model from server', function () {
beforeEach(function () {
this.fixture = this.fixtures.Messages.valid;
this.fixtureResponse = this.fixture.response.messages[0];
this.server = sinon.fakeServer.create();
this.server.respondWith(
'GET',
// some url
JSON.stringify(this.fixtureResponse)
);
});
it('should the recipient_user be defined', function () {
this.model.fetch();
this.server.respond();
// this.fixtureResponse.recipient_user is not defined
// as expected by the relation defined in (3)
expect(this.fixtureResponse.recipient_user).toBeDefined();
});
});
});
});
Run Code Online (Sandbox Code Playgroud)
看看这个系列教程http://tinnedfruit.com/2011/03/03/testing-backbone-apps-with-jasmine-sinon.html
这是模型测试的具体部分。
不知道是否能解决您的问题,但可能包含宝贵的信息。
| 归档时间: |
|
| 查看次数: |
748 次 |
| 最近记录: |