使用Jasmine测试RequireJS

Testing RequireJS with Jasmine

本文关键字:RequireJS 测试 Jasmine 使用      更新时间:2023-09-26

我对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();
    });
});

但是当我运行它时,我得到一个引用错误,茉莉找不到变量b。我错过了什么吗?

如果您能给我指路,我将不胜感激。

尝试如下:

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();
    });
});

我不太了解Jasmine,但是你不能在require()呼叫中进行describe()测试吗?

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