简体   繁体   English

用Jasmine测试RequireJS

[英]Testing RequireJS with Jasmine

I'm very new to Javascript and I'm trying to write tests for a project I've joined in on. 我是Java语言的新手,我正为参与的项目编写测试。 I have files in the program that look like this: 我在程序中有如下文件:

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

And I can't figure out how to access these functions in a test. 我无法弄清楚如何在测试中访问这些功能。 I've tried instantiating an object (although I may be doing it wrong) and calling the functions from there like this: 我尝试实例化一个对象(尽管我可能做错了),然后从那里调用函数,如下所示:

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

But when I run it I get a reference error that Jasmine cannot find the variable b. 但是当我运行它时,我收到一个参考错误,Jasmine无法找到变量b。 Is there something that I'm missing? 有什么我想念的吗? Any help to point me in the right direction would be appreciated. 向我指出正确方向的任何帮助将不胜感激。

Try the following: 请尝试以下操作:

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

I don't know Jasmine wery well, but can't you just describe() the test inside of a require() call? 我不太了解茉莉花,但您不能只在require()调用中describe()测试吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM