2015-10-20 84 views
1

我目前正在使用IIFE(立即調用的函數表達式)編寫驅動程序腳本的簡單測試用例。這是我的驅動程序腳本。使用IIFE進行角度單位測試會拋出一個參考錯誤?

driver.js

(function() { 
"use strict"; 

var app = angular 
    .module("myApp", [ 
     "ui.bootstrap", 
     "ui.sortable" 
    ]); 
}()); 

這裏是我的規格driver.spec.js

describe("application configuration tool driver", function() { 
it("should create an angular module named myTest", function() { 
    expect(app).toEqual(angular.module("myApp")); 
}); 
}); 

當我運行使用IIFE我的規格。我得到一個ReferenceError:應用程序沒有定義。

如果我運行驅動程序的腳本不IIFE:

var app = angular 
    .module("myApp", [ 
     "ui.bootstrap", 
     "ui.sortable" 
    ]); 

我的規格通行證。有關使用IIFE傳遞規範的想法?

+1

變量'app'是本地IIFE,但測試'希望(應用程序)。 toEqual(angular.module(「myApp」));'對我沒有意義。 – PSL

+0

爲什麼你甚至需要變量'應用程序'? – charlietfl

+0

我正在使用變量'app'來創建自定義指令和控制器。 – Teja

回答

0

可以移動app回外部範圍(如果那是當然,你可以把一個選項):

var app; 
(function(app) { 
    "use strict"; 

    app = angular 
     .module("myApp", [ 
      "ui.bootstrap", 
      "ui.sortable" 
     ]); 
}(app)); 
相關問題