jef*_*own 12 unit-testing jasmine angularjs karma-runner
我是茉莉和业力的新手.我相信我已经正确设置了环境,并且我能够运行非常基本的单元测试,但是一旦我尝试实例化控制器,我就会收到一个未知的提供程序错误,我不确定如何调试它.我是否需要传递stateProvider依赖项?我没有在angular-seed示例中看到这一点.
Bower.json:
{
"name": "starter",
"description": "A starter project for AngularJS",
"version": "2.0.0",
"homepage": "https://starter.com",
"private": true,
"dependencies": {
"angular": "1.2.x",
"angular-route": "1.2.x",
"angular-loader": "1.2.x",
"angular-mocks": "~1.2.15"
}
}
Run Code Online (Sandbox Code Playgroud)
家庭控制器:
angular.module('home').controller('Home', function($scope, $rootScope, $state) {
console.log($scope.pageType);
$rootScope.pageType = 'home';
/*
* Takes in a state and transitions the app to that state.
*/
$scope.goTo = function(value) {
$state.transitionTo(value);
}
/*
* Handles what happens after clicking log-in
*/
$scope.loginClicked = function() {
$state.transitionTo('log-in');
}
});
Run Code Online (Sandbox Code Playgroud)
测试文件:
'use strict';
/* jasmine specs for controllers go here */
describe('Home', function() {
beforeEach(module('home'));
it('should run tests', inject(function() {
expect(null).toBeDefined();
}));
it('should not say true equals false', function() {
expect(false).not.toBe(true);
});
it('should say true equals true', function() {
expect(true).toBe(true);
});
it('should say false does not equal true', function() {
expect(false).not.toBe(true);
});
it('should create "phones" model with 3 phones', inject(function($controller,$rootScope) {
/*
*
* COMMENTING OUT THESE LINES = PASS
*
*/
var scope = $rootScope.$new(),
ctrl = $controller('Home', {$scope:scope});
expect(ctrl).not.toBe(null);
}));
});
Run Code Online (Sandbox Code Playgroud)
错误:
Error: [$injector:unpr] Unknown provider: $stateProvider <- $state
http://errors.angularjs.org/1.2.16/$injector/unpr?p0=%24stateProvider%20%3C-%20%24state
at /Users/jlett/bower_components/angular/angular.js:78:12
at /Users/jlett/bower_components/angular/angular.js:3705:19
at Object.getService [as get] (/Users/jlett/bower_components/angular/angular.js:3832:39)
at /Users/jlett/bower_components/angular/angular.js:3710:45
at getService (/Users/jlett/bower_components/angular/angular.js:3832:39)
at invoke (/Users/jlett/bower_components/angular/angular.js:3859:13)
at Object.instantiate (/Users/jlett/bower_components/angular/angular.js:3880:23)
at /Users/jlett/bower_components/angular/angular.js:7134:28
at null.<anonymous> (/Users/jlett/test/unit/home-controller_tests.js:26:20)
at Object.invoke (/Users/jlett/bower_components/angular/angular.js:3869:17)
Error: Declaration Location
at window.inject.angular.mock.inject (/Users/jlett/bower_components/angular-mocks/angular-mocks.js:2132:25)
at null.<anonymous> (/Users/jlett/test/unit/home-controller_tests.js:24:54)
at /Users/jlett/zoetis-rimadyl-mobile/test/unit/home-controller_tests.js:5:1
Run Code Online (Sandbox Code Playgroud)
Jim*_*ert 36
如果未包含其中一个注射模块,您将收到此错误.
例如,你有
beforeEach(module('home'));
Run Code Online (Sandbox Code Playgroud)
如果您的$state依赖项不在home模块中,则还需要包含该模块.我不熟悉$state(我认为它是angular-ui的路由器?只有angular.js服务才应该开始$).如果它是角度ui,这就是你应该如何设置:
beforeEach(module('ui.router'));
beforeEach(module('home'));
Run Code Online (Sandbox Code Playgroud)
这样,angular的测试运行器就知道运行测试需要哪些模块.
实际上,home只要您将ui.router依赖项定义为该模块的依赖项,包含该模块就应该为您执行此操作.如果配置正确,则可能需要查看包含在测试中的文件的顺序.例如,确保测试中包含ui-router文件,并且home在karma的配置中在模块之前引用它.
当您$state在控制器中包含依赖项时 - 您需要$state在控制器单元测试中提供.
var $scope = $rootScope.$new(),
ctrl = $controller('Home', {
$scope: $scope,
$state: {} //Or inject the state using the injector service and then you can use some jasmine spies to mock the function calls or just to spy on 'em.
});
expect(ctrl).not.toBe(null);
Run Code Online (Sandbox Code Playgroud)
你的阻止与变化....
it('should create "phones" model with 3 phones', inject(function($controller, $rootScope, $state) {
/*
*
* COMMENTING OUT THESE LINES = PASS
*
*/
var scope = $rootScope.$new(),
ctrl = $controller('Home', {$scope:scope, $state: $state});
expect(ctrl).not.toBe(null);
}));
Run Code Online (Sandbox Code Playgroud)
然而在我的单元测试的设置我很喜欢,因为我在此设置描述创建控制器设置功能在这里.
| 归档时间: |
|
| 查看次数: |
15586 次 |
| 最近记录: |