我是單元測試和AngularJS的新手,我有一些問題我無法解決。我的一個測試不起作用。我試圖通過影響一個值在我的測試中啓動location.path(),但在我的控制器中,location.path()仍然有一個未定義的值。
這裏是我的CONTROLER:
angular.module('...')
.controller('SignUpCtrl', ['$location', function ($location) {
// Retrieve type of user
var userType = $location.path().substr(9);
if(userType == 'member'){
userType = 'user';
}
console.log($location.path());
console.log(userType);
$scope.uType = userType; ]);
這裏是我的測試模塊:
describe('Controller: SignUpCtrl', function() {
// load the controller's module
beforeEach(module('...'));
var SignUpCtrl,
scope,
mockBackend,
environments,
location,
store;
beforeEach(inject(function ($controller, $rootScope, $httpBackend,$location,_Environments_) {
environments = _Environments_;
mockBackend = $httpBackend;
location = $location;
scope = $rootScope.$new();
SignUpCtrl = $controller('SignUpCtrl', {
$scope: scope,
$location: location
});
}));
it('should come from the right location', function(){
location.path('/sign-up/member');
expect(location.path()).toBe('/sign-up/member');
expect(scope.uType).toBe('user'); //Do not work
});
});
感謝您的迴應我會看看E2E測試,因爲它仍然讓我非常困惑在單元測試中測試什麼以及E2E中的什麼... – user2876974