我正在用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