Jasmine angularjs - 监视初始化控制器时调用的方法
我目前正在使用 Jasmine 和 Karma(Testacular) 和 Web Storm 来编写单元测试.我无法监视初始化控制器时立即调用的方法.是否可以窥探控制器初始化时调用的方法?
I am currently using Jasmine with Karma(Testacular) and Web Storm to write unit test. I am having trouble spying on a method that gets called immediately when the controller is initialized. Is it possible to spy on a method that is called when the controller is initialized?
我的控制器代码,我试图监视的方法是 getServicesNodeList()
.
My controller code, the method I am attempting to spy on is getServicesNodeList()
.
myApp.controller('TreeViewController', function ($scope, $rootScope ,$document, DataServices) {
$scope.treeCollection = DataServices.getServicesNodeList();
$rootScope.viewportHeight = ($document.height() - 100) + 'px';
});
这是测试规范:
describe("DataServices Controllers - ", function () {
beforeEach(angular.mock.module('myApp'));
describe("DataServicesTreeview Controller - ", function () {
beforeEach(inject(function ($controller, $rootScope, $document, $httpBackend, DataServices) {
scope = $rootScope.$new(),
doc = $document,
rootScope = $rootScope;
dataServices = DataServices;
$httpBackend.when('GET', '/scripts/internal/servicedata/services.json').respond(...);
var controller = $controller('TreeViewController', {$scope: scope, $rootScope: rootScope, $document: doc, DataServices: dataServices });
$httpBackend.flush();
}));
afterEach(inject(function($httpBackend){
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
}));
it('should ensure DataServices.getServicesNodeList() was called', inject(function ($httpBackend, DataServices) {
spyOn(DataServices, "getServicesNodeList").andCallThrough();
$httpBackend.flush();
expect(DataServices.getServicesNodeList).toHaveBeenCalled();
}));
});
});
测试失败,说明该方法没有被调用.我知道我应该模拟 DataServices
并将其传递给测试控制器.但是,无论它是否是模拟方法,在监视该方法时,我似乎仍然会遇到同样的问题.任何人有任何想法或可以指出我处理此问题的正确方法的资源?
The test is failing saying that the method has not been called. I know that I should mock the DataServices
and pass that into the test controller. But it seems like I would still have the same problem when spying on that method whether it is a mock or not. Anyone have any ideas or could point me to resources on the correct way to handle this?
推荐答案
在编写单元测试时,应该隔离每一段代码.在这种情况下,您需要隔离您的服务并单独对其进行测试.创建服务的模拟并将其传递给您的控制器.
When writing unit tests, you should isolate each piece of code. In this case, you need to isolate your service and test it separately. Create a mock of the service and pass it to your controller.
var mockDataServices = {
getServicesNodeList: function () {
return <insert your sample data here > ;
}
};
beforeEach(inject(function ($controller, $rootScope, $document) {
scope = $rootScope.$new(),
doc = $document,
rootScope = $rootScope;
var controller = $controller('TreeViewController', {
$scope: scope,
$rootScope: rootScope,
$document: doc,
DataServices: mockDataServices
});
}));
如果发出 $http 请求的是您的服务,您可以从单元控制器测试中删除该部分.编写另一个单元测试来测试服务在初始化时是否进行了正确的 http 调用.
If it is your service that is making the $http request, you can remove that portion from your unit controller test. Write another unit test that tests that the service is making the correct http calls when it is initialized.
相关文章