Angularjs 角度Karma控制器测试:UI路由器持久$stateparms依赖项
我正试图围绕控制器构建一些测试。我正在使用$stateParams注入一个测试,并导航到同一个状态两次。第一个测试通过,但第二个测试失败,因为它认为creationId仍然为1Angularjs 角度Karma控制器测试:UI路由器持久$stateparms依赖项,angularjs,testing,angular-ui-router,mocha.js,karma-runner,Angularjs,Testing,Angular Ui Router,Mocha.js,Karma Runner,我正试图围绕控制器构建一些测试。我正在使用$stateParams注入一个测试,并导航到同一个状态两次。第一个测试通过,但第二个测试失败,因为它认为creationId仍然为1 it('should navigate to customizer with a creation ID', function (done) { inject(function ($state, $stateParams, $rootScope) { $state.go('
it('should navigate to customizer with a creation ID', function (done) {
inject(function ($state, $stateParams, $rootScope) {
$state.go('customizer', {creationId: 1});
$rootScope.$digest();
expect($stateParams).to.have.property('creationId','1');
done();
});
});
it('should navigate to customizer without a creation ID', function (done) {
inject(function ($state, $stateParams, $rootScope) {
$state.go('customizer');
$rootScope.$digest();
expect($stateParams).to.not.have.property('creationId','1');
done();
});
});
为什么会这样?是否需要在每次之前或之后运行某个程序来清除状态?如问题注释中所述,为了让测试执行控制器的测试,
$state
应该模拟出来,并以某种方式验证是否发生了状态转换
这就是我们一直在使用的:
从一个规范可以让你对如何使用它有一个合理的认识
describe('state', function() {
it('should transition correctly on invoking previous', function() {
state.expectTransitionTo('month', {
year: 2015,
month: 7
});
$scope.previous();
state.ensureAllTransitionsHappened();
});
it('should transition correctly on invoking next', function() {
state.expectTransitionTo('month', {
year: 2015,
month: 9
});
$scope.next();
state.ensureAllTransitionsHappened();
});
it('should transition correctly on month change', function() {
state.expectTransitionTo('month', {
year: 2015,
month: 1
});
calendar.month = 1;
$scope.$digest();
state.ensureAllTransitionsHappened();
});
it('should transition correctly on year change', function() {
state.expectTransitionTo('month', {
year: 1979,
month: 8
});
calendar.year = 1979;
$scope.$digest();
state.ensureAllTransitionsHappened();
});
});
如问题注释中所述,为了让测试完成控制器的测试,
$state
应该模拟出来,并以某种方式验证是否发生了状态转换
这就是我们一直在使用的:
从一个规范可以让你对如何使用它有一个合理的认识
describe('state', function() {
it('should transition correctly on invoking previous', function() {
state.expectTransitionTo('month', {
year: 2015,
month: 7
});
$scope.previous();
state.ensureAllTransitionsHappened();
});
it('should transition correctly on invoking next', function() {
state.expectTransitionTo('month', {
year: 2015,
month: 9
});
$scope.next();
state.ensureAllTransitionsHappened();
});
it('should transition correctly on month change', function() {
state.expectTransitionTo('month', {
year: 2015,
month: 1
});
calendar.month = 1;
$scope.$digest();
state.ensureAllTransitionsHappened();
});
it('should transition correctly on year change', function() {
state.expectTransitionTo('month', {
year: 1979,
month: 8
});
calendar.year = 1979;
$scope.$digest();
state.ensureAllTransitionsHappened();
});
});
如问题注释中所述,为了让测试完成控制器的测试,
$state
应该模拟出来,并以某种方式验证是否发生了状态转换
这就是我们一直在使用的:
从一个规范可以让你对如何使用它有一个合理的认识
describe('state', function() {
it('should transition correctly on invoking previous', function() {
state.expectTransitionTo('month', {
year: 2015,
month: 7
});
$scope.previous();
state.ensureAllTransitionsHappened();
});
it('should transition correctly on invoking next', function() {
state.expectTransitionTo('month', {
year: 2015,
month: 9
});
$scope.next();
state.ensureAllTransitionsHappened();
});
it('should transition correctly on month change', function() {
state.expectTransitionTo('month', {
year: 2015,
month: 1
});
calendar.month = 1;
$scope.$digest();
state.ensureAllTransitionsHappened();
});
it('should transition correctly on year change', function() {
state.expectTransitionTo('month', {
year: 1979,
month: 8
});
calendar.year = 1979;
$scope.$digest();
state.ensureAllTransitionsHappened();
});
});
如问题注释中所述,为了让测试完成控制器的测试,
$state
应该模拟出来,并以某种方式验证是否发生了状态转换
这就是我们一直在使用的:
从一个规范可以让你对如何使用它有一个合理的认识
describe('state', function() {
it('should transition correctly on invoking previous', function() {
state.expectTransitionTo('month', {
year: 2015,
month: 7
});
$scope.previous();
state.ensureAllTransitionsHappened();
});
it('should transition correctly on invoking next', function() {
state.expectTransitionTo('month', {
year: 2015,
month: 9
});
$scope.next();
state.ensureAllTransitionsHappened();
});
it('should transition correctly on month change', function() {
state.expectTransitionTo('month', {
year: 2015,
month: 1
});
calendar.month = 1;
$scope.$digest();
state.ensureAllTransitionsHappened();
});
it('should transition correctly on year change', function() {
state.expectTransitionTo('month', {
year: 1979,
month: 8
});
calendar.year = 1979;
$scope.$digest();
state.ensureAllTransitionsHappened();
});
});
您确实应该为
$state
、$stateParams
和其他可注入项使用mock/spies。实际上,您在这里所做的是测试已经经过良好测试的ui.router
。您确实应该为$state
、$stateParams
和其他可注入项使用mock/spies。实际上,您在这里所做的是测试已经经过良好测试的ui.router
。您确实应该为$state
、$stateParams
和其他可注入项使用mock/spies。实际上,您在这里所做的是测试已经经过良好测试的ui.router
。您确实应该为$state
、$stateParams
和其他可注入项使用mock/spies。你在这里主要做的是测试已经很好测试过的ui.router
,我只是对它需要做的工作感到惊讶。。。所以我应该为我所有的角度依赖性编写模拟?甚至像$location或$upload(ng文件上传)?@ArdentKid-这取决于具体情况。如果我们正在测试控制器,并且您不希望任何原始依赖项干扰测试,那么我们应该使用mock。否则,使用真正的依赖项不应该是一个问题。我只是对它将花费的工作量感到惊讶。。。所以我应该为我所有的角度依赖性编写模拟?甚至像$location或$upload(ng文件上传)?@ArdentKid-这取决于具体情况。如果我们正在测试控制器,并且您不希望任何原始依赖项干扰测试,那么我们应该使用mock。否则,使用真正的依赖项不应该是一个问题。我只是对它将花费的工作量感到惊讶。。。所以我应该为我所有的角度依赖性编写模拟?甚至像$location或$upload(ng文件上传)?@ArdentKid-这取决于具体情况。如果我们正在测试控制器,并且您不希望任何原始依赖项干扰测试,那么我们应该使用mock。否则,使用真正的依赖项不应该是一个问题。我只是对它将花费的工作量感到惊讶。。。所以我应该为我所有的角度依赖性编写模拟?甚至像$location或$upload(ng文件上传)?@ArdentKid-这取决于具体情况。如果我们正在测试控制器,并且您不希望任何原始依赖项干扰测试,那么我们应该使用mock。否则,使用真正的依赖项不应该是问题。