2
測試後我有角服務,調用Web API方法:Angularjs單位測試不打然後控制器的一部分在AngularJS呼叫服務及使用噶&茉莉
services.service('loginService', function ($http) { this.loginAction = function (userInfo) { console.log(userInfo);
return $http({
method: 'POST',
url: 'url of web api service',
params: userInfo
});
} });
我有控制器,其調用此服務&中獲取數據,則框
adminApp.controller('loginController', ['$scope','loginService','localStorageService','$location', function ($scope, loginService, localStorageService, $location) {
$scope.login = function (isValid) {
var userData = { 'username': $scope.username, 'password': $scope.password };
if (isValid) {
loginService.loginAction(userData).then(
function (response) {
console.log(response);
if (response.data.Success) {
alert("Success !!!!");
IsAdminMenu = true;
localStorageService.clearAll();
localStorageService.set('Token', response.data.Data);
$scope = $scope || angular.element(document).scope();
if (!$scope.$$phase) {
//this will kickstart angular if to notice the change
$scope.$apply();
}
$location.path('/dashboard/');
}
else {
alert(response.data.Data);
}
}, function (error) {
alert('Got the error');
});
}
}
} ]);
其工作良好,但,當我使用利用因緣+茉莉寫入單元測試來測試該控制器
describe('LogIn in Progress....', function() {
var scope, $controller, ctrl, $rootScope;//use this scope in tests
//mock Application to allow us to inject our own dependencies
beforeEach(module('adminApp'));
//mock the controller for the same reason and include $rootScope and $controller
beforeEach(inject(function ($rootScope, $controller) {
//create an empty scope
scope = $rootScope.$new();
//declare the controller and inject our empty scope
ctrl = $controller('loginController', { $scope: scope });
}));
// tests start here
it('Check for Valid LogIn', function() {
scope.username = 'admin';
scope.password = 'test12';
scope.login(true);
expect(scope.IsAdminMenu).toEqual(true);
});
});
當我運行這個它不會進入。然後控制器的一部分 console.log(response);沒有得到響應
我應該在我的測試 – user2089961
必須合併我的代碼以測試並incjet $ Q服務做什麼修改。在這條線loginService.loginAction = jasmine.createSpy( 'loginService.loginAction') –
geetting誤差and.returnValue(action.promise)。我正在使用茉莉花茉莉花版本:1.3.1修訂版1354556913 – user2089961