簡體   English   中英

用Jasmine測試RequireJS

[英]Testing RequireJS with Jasmine

我是Java語言的新手,我正為參與的項目編寫測試。 我在程序中有如下文件:

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。 有什么我想念的嗎? 向我指出正確方向的任何幫助將不勝感激。

請嘗試以下操作:

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

我不太了解茉莉花,但您不能只在require()調用中describe()測試嗎?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM