我正在通过 jasmine 使用骨干和骨干.marionette 为 Web 应用程序编写测试。
我的问题是:
1)我应该在模型发生变化时检查视图以查看视图是否受到影响?
2)如果是,那么正确的方法是什么……。例如在以下情况(1)
PS:
我想避免更改模板文件
(1)
// template
<ul class="widget-points-status">
<li>
<strong>{{ remaining_points }}</strong>
</li>
<li>
<strong>{{ given_points }}</strong>
</li>
<li>
<strong>{{ received_points }}</strong>
</li>
</ul>
// My jasmine test could be:
describe('Changing the model:', function () {
beforeEach(function () {
app.currentUser.set({
given_points: 111980,
received_points: 892378,
remaining_points: 435412
});
});
it('it should change the points', function () {
var pointsView = this.view.$el.text().match(/\d{1,}/)[0];
expect(pointsView).toMatch(app.currentUser.get('given_points'));
expect(pointsView).toMatch(app.currentUser.get('received_points'));
expect(pointsView).toMatch(app.currentUser.get('remaining_points'));
});
});
var pointsView = Backbone.View.extend({
className: 'widget widget-profile',
initialize: function () {
app.currentUser.on('change', this.render, this);
},
render: function () {
this.$el.html(profileTemplate(app.currentUser.toJSON()));
return this;
}
});