我無法獲得一個簡單的Jasmine測試來處理我的AngularJS模塊。任何建議將非常感激。用Jasmine測試AngularJS模塊導致Jasmine錯誤
模塊與控制器:
(function(myApp) {
myApp.App = angular.module("MyApp", []).
config(["$routeProvider", function($routeProvider) {
$routeProvider.
when('/', { controller: "CoreAppController", templateUrl: 'App.html' }).
otherwise({ redirectTo: '/' });
}]);
myApp.App.controller("CoreAppController", function($scope, $http, widgetService) {
/* controller definition */
}
}(window.myApp = window.myApp || {}));
單元測試:
(function(myApp) {
describe("CoreAppController spec", function() {
describe("Create the CoreApp", function() {
beforeEach(angular.module("MyApp"));
describe("CoreAppController", function() {
it("Should create the core application",
expect(1).toBeGreaterThan(0)
)
});
})
});
}(window.myApp = window.myApp || {}));
文件包括在測試文件是順序如下:
- jasmine.js
- jasmine- html.js
- angular.min.js
- angular.mock.js
- app.js(包含模塊和控制器的定義)
- CoreAppControllerSpec.js(單元測試文件)。
運行該測試結果在下面的錯誤:
類型錯誤:對象#沒有方法在jasmine.Block.execute '應用' (文件:/// C:/ TeamFoundation/PRE /測試/jasmine/jasmine.js:1064:17) at jasmine.Queue.next_(file:/// C:/TeamFoundation/PRE/test/jasmine/jasmine.js:2096:31) at jasmine.Queue.start (file:/// C:/TeamFoundation/PRE/test/jasmine/jasmine.js:2049:8) at jasmine.Spec.execute(file:/// C:/ TeamFoundation/PRE/test/jasmine/jasmine .js:2376:14) at jasmine.Queue.next_(file:/// C:/TeamFoundation/PRE/test/jasmine/jasmine.js:2096:31) at jasmine.Queue.start(file:/// C:/TeamFoundation/PRE/test/jasmine/jasmine.js:2049:8) at jasmine.Suite.execute(file:/// C:/ TeamFoundation/PRE/test/jasmine/jasmine.js:2521:14) at jasmine.Queue.next_(file:/// C:/TeamFoundation/PRE/test/jasmine/jasmine.js:2096:31) at jasmine。 Queue.start(file:/// C:/TeamFoundation/PRE/test/jasmine/jasmine.js:2049:8) at jasmine.Suite.execute(file:/// C:/ TeamFoundation/PRE/test/jasmine/jasmine.js:2521:14)
從測試中刪除'module'行會導致它通過。我究竟做錯了什麼?
刪除了'myApp'匿名函數包裝並更改了代碼以將控制器直接添加到angular.module(「MyApp」),並且仍然出現單元測試失敗並出現相同錯誤。 – jwest