使用Jasmine测试RequireJS

时间:2014-07-21 18:02:28

标签: javascript requirejs jasmine

我是Javascript的新手,我正在尝试为我加入的项目编写测试。我在程序中的文件看起来像这样:

define([
  'jquery',
  'underscore',
  'backbone',
  'backbone/models/beat',
  'colors',
  'app/dispatch',
  'app/log'
], function($, _, Backbone, BeatModel, COLORS, dispatch, log){
  return Backbone.View.extend({

    getOpacityNumber : function(bool) {
      //code
    },

    unroll: function(){
      //code
    }
  });
});

我无法弄清楚如何在测试中访问这些功能。我试过实例化一个对象(虽然我可能做错了)并从那里调用函数:

describe("beatView.js", function() {
    beforeEach( function() {
            var b = new beatView();
    });

    spyOn(console, "log");

    it("test the console log", function() {
        b.unroll();
        expect(console.log).toHaveBeenCalled();
    });
});

但是当我运行它时,我得到一个引用错误,Jasmine无法找到变量b。有什么东西我不见了吗?任何有助于指出我正确方向的帮助都将受到赞赏。

2 个答案:

答案 0 :(得分:1)

尝试以下方法:

describe("beatView.js", function() {
    var b = null;
    beforeEach( function() {
            b = new beatView();
    });

    spyOn(console, "log");

    it("test the console log", function() {
        b.unroll();
        expect(console.log).toHaveBeenCalled();
    });
});

答案 1 :(得分:0)

我不太了解Jasmine很好,但是你只能在describe()电话内require()进行测试吗?

require(["beatView"], function (beatView) {
  describe(...);
});