我的登录代码.js是
var loginModule = angular.module('loginModule', [])
.controller('LoginCtrl', function ($scope, $auth, $location) {
if (!$auth.isAuthenticated()) {
$scope.oneAtATime = true;
$scope.login = function () {
$auth.login({userName: $scope.username, password: $scope.password, isEncript: false})
.then(function () {
console.log($auth.getMessage());
})
.catch(function (response) {
console.log(response);
});
};
}
else {
$location.path('/home');
}
$scope.status = 'true';
});
单元测试代码为
describe('login()', function() {
it('should be able to handle login errors', function () {
var user = {
email: 'foo@bar.com',
password: '1234',
isEncript: false
};
this.$httpBackend.expectPOST('app/controller/apicall.php?serviceName=login').respond(200);
this.$auth.login(user).then(angular.noop, function () {
});
this.$httpBackend.flush();
expect(user.isEncript).toBe(false);
});
});
});
......................我得到的错误如下
$auth login() should be able to handle login errors FAILED
Error: Unexpected request: POST ../../controller/apicall.php?serviceName=login
Expected POST /app/controller/apicall.php?serviceName=login
帮助我解决我应该如何解决此错误。
有东西在召唤
../../controller/apicall.php?serviceName=login
你可以弄清楚什么叫它,或者只是改变你的
this.$httpBackend.expectPOST('app/controller/apicall.php?serviceName=login').respond(200);
自
this.$httpBackend.expectPOST('../../controller/apicall.php?serviceName=login').respond(200);
这应该可以做到。
此错误表示您的$auth服务将请求发送到其他 URL。因此,首先您需要检查并测试当您调用其"登录"方法时,您的$auth服务做什么。
更好的测试方法是为$auth服务(您将在其中测试它如何与后端交互)和 LoginCtrl 控制器(您将在其中模拟$auth服务并仅测试控制器的行为)使用单独的套件。
您可以使用此 plunker http://tinyurl.com/lv3gskf 为例。(我使用 tinyurl.com 因为堆栈溢出不允许在没有代码的情况下发布指向 plunker 的链接)