2014-07-21 89 views
0

我對JavaScript很陌生,我正在爲我加入的項目編寫測試。我在程序中的文件看起來像這樣:用Jasmine測試RequireJS

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。有什麼我失蹤了嗎?任何幫助指出我在正確的方向,將不勝感激。

回答

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

好吧,現在可以找到變量b,但問題是,現在我得到'ReferenceError:無法找到變量:beatView'我不知道如何引用該對象。 – JoeBruzek

0

我不知道茉莉花韋裏好,但你不能只describe()一個require()調用內部的測試?

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