Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 检查用Jasmine调用的主干视图方法_Backbone.js_Jasmine - Fatal编程技术网

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>');