spec.js
'use strict';
describe('homeView', function () {
var $componentController;
var controller;
// loads the module
beforeEach(module('homeView'));
beforeEach(inject(function ($injector) {
$componentController = $injector.get('$componentController');
controller = $componentController('homeView', {
$scope: {},
$route: {},
EmpApi: "What should I write here???"
});
}));
//beforeEach(inject(function (_$componentController_) {
// $componentController = _$componentController_;
//}));
it('test number', function () {
expect(controller.number).toEqual(7);
});
});
模块
angular.module('homeView', []);
组件
angular.
module('homeView').
component('homeView', {
templateUrl: 'home-view/home-view.template.html',
controller: ['$scope', '$route', 'EmpApi',
function HomeViewController($scope, $route, EmpApi) {
this.number = 7;
}
]
});
EmpApi是从工厂返回的对象。如何对该参数为$ scope,$ route和EmpApi的控制器进行单元测试?如果删除有关route和Empapi的所有内容,则它通过了测试。当我保留它们时,会出现此错误
TypeError: EmpApi.getInterviewees is not a function
at <Jasmine>
at getInterviewees (C:/Projects/IntervieweeApp/IntervieweeSPA/IntervieweeSPA/home-view/home-view.component.js:15:28)
at new HomeViewController (C:/Projects/IntervieweeApp/IntervieweeSPA/IntervieweeSPA/home-view/home-view.component.js:13:17)
at Object.instantiate (C:/Projects/IntervieweeApp/IntervieweeSPA/IntervieweeSPA/Scripts/angular.js:5137:14)
at $controller (C:/Projects/IntervieweeApp/IntervieweeSPA/IntervieweeSPA/Scripts/angular.js:11688:28)
at C:/Projects/IntervieweeApp/IntervieweeSPA/IntervieweeSPA/Scripts/angular-mocks.js:2555:14
at $componentController (C:/Projects/IntervieweeApp/IntervieweeSPA/IntervieweeSPA/Scripts/angular-mocks.js:2611:14)
at UserContext.<anonymous> (C:/Projects/IntervieweeApp/IntervieweeSPA/IntervieweeSPA/home-view/home-view.component.spec.js:13:22)
at Object.invoke (C:/Projects/IntervieweeApp/IntervieweeSPA/IntervieweeSPA/Scripts/angular.js:5122:19)
at UserContext.WorkFn (C:/Projects/IntervieweeApp/IntervieweeSPA/IntervieweeSPA/Scripts/angular-mocks.js:3459:20)
at <Jasmine>
at window.inject.angular.mock.inject (C:/Projects/IntervieweeApp/IntervieweeSPA/IntervieweeSPA/Scripts/angular-mocks.js:3422:25)
at Suite.<anonymous> (C:/Projects/IntervieweeApp/IntervieweeSPA/IntervieweeSPA/home-view/home-view.component.spec.js:11:16)
at <Jasmine>
at C:/Projects/IntervieweeApp/IntervieweeSPA/IntervieweeSPA/home-view/home-view.component.spec.js:3:1
TypeError: Cannot read property 'number' of undefined
at <Jasmine>
at UserContext.<anonymous> (C:/Projects/IntervieweeApp/IntervieweeSPA/IntervieweeSPA/home-view/home-view.component.spec.js:28:27)
at <Jasmine>
getInterviewees是EmpApi中的一个函数,在这段测试代码中我什至没有调用它。