問題描述
我目前正在使用 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.
這篇關于Jasmine angularjs - 監視初始化控制器時調用的方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!