我正在用Jasmine和Karma測試我的angularjs應用程序。測試時未執行角度js控制的函數
我的測試是這樣的:
describe('Login test', function() {
// Mock our module in our tests
beforeEach(module('Project'));
var ctrl, scope;
// inject the $controller and $rootScope services
// in the beforeEach block
beforeEach(inject(function($controller, $rootScope) {
// Create a new scope that's a child of the $rootScope
scope = $rootScope.$new();
// Create the controller
ctrl = $controller('loginController', {
$scope: scope
});
}));
it('should get login success',function() {
scope.loginClick('user', 'pass');
});
});
我與loginClick功能的登錄控制器,而這個函數裏面我有一個正在POST請求另一個功能。問題是內部函數永遠不會執行,我嘗試console.log()來查看函數是否被調用,但沒有成功。 我的函數如下:
app.controller('loginController', ['$scope', '$http', '$route', function ($scope, $http, $route) {
console.log('controller call'); // yes it works
...
$scope.loginClick = function (username, password) {
console.log('controller call'); // yes it works
handler.reqPOST('/login', userData, function (result) {
console.log('login request'); // no output is sent to the console
});
};
}]);
的處理對象是包括在啓動的業力配置文件中
如果reqPOST發出http請求,則需要使用ngMock中的$ httpBackend服務設置模擬http後端。 – Chandermani 2014-10-27 11:32:40