Backbone.js 检查用Jasmine调用的主干视图方法
我正在开发一个主干应用程序,其中所有功能都在Backbone.js 检查用Jasmine调用的主干视图方法,backbone.js,jasmine,Backbone.js,Jasmine,我正在开发一个主干应用程序,其中所有功能都在App下命名。我有一个名为App.LoginView的视图,我想用Jasmine来测试它,但我正在努力让它工作 这是测试的代码: describe("Test the views", function () { 'use strict'; beforeEach(function () { // Create DOM element $('body').append('<div class="con
App
下命名。我有一个名为App.LoginView
的视图,我想用Jasmine来测试它,但我正在努力让它工作
这是测试的代码:
describe("Test the views", function () {
'use strict';
beforeEach(function () {
// Create DOM element
$('body').append('<div class="content"></div>');
});
afterEach(function () {
$('div.content').remove();
});
// Test the LoginView object was created
it("tests that the LoginView object exists", function () {
// Check the view definition exists
expect(App.LoginView).toBeDefined();
// Spy on the prototype
spyOn(App, 'LoginView').andCallThrough();
// Create the view
this.LoginView = new App.LoginView();
// Check the view exists
expect(this.LoginView).toBeDefined();
expect(this.LoginView.initialize).toBeDefined();
expect(this.LoginView.template).toBeDefined();
expect(this.LoginView.tagName).toBeDefined();
expect(this.LoginView.render).toBeDefined();
// Remove it
this.LoginView.remove();
});
});
我正在使用
grunt contrib jasmine
运行测试,并使用jasmine jquery
添加对jquery的支持。过去我用过一点茉莉花,但我很难看出哪里出了问题。最终找到了解决办法。为了让它工作,我观察了原型的initialize
方法,并更改了调用through的语法,正如Jasmine 2中所做的更改一样
spyOn(App.LoginView.prototype, 'initialize').and.callThrough();
我还使用jasmine jquery
动态地将一个基本模板插入到DOM中,因为缺少模板:
$('body').append('<script type="text/template" id="login-template"><div class="content-padded"></div></script>');
$('body')。追加(“”);
$('body').append('<script type="text/template" id="login-template"><div class="content-padded"></div></script>');