Jasmine angularjs - 监视初始化控制器时调用的方法

Jasmine angularjs - spying on a method that is called when controller is initialized(Jasmine angularjs - 监视初始化控制器时调用的方法)
本文介绍了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.

这篇关于Jasmine angularjs - 监视初始化控制器时调用的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Drag amp; Drop with Protractor by Repeater(拖动amp;通过中继器使用量角器掉落)
Getting the position of the element in a list when it#39;s drag/dropped (ui.sortable)(拖放时获取元素在列表中的位置(ui.sortable))
Detecting HTML5 Drag And Drop support in javascript(在 javascript 中检测 HTML5 拖放支持)
HTML5 drop event doesn#39;t work unless dragover is handled(除非处理了拖动,否则 HTML5 放置事件不起作用)
How to use jQuery#39;s drop event to upload files dragged from the desktop?(如何使用 jQuery 的 drop 事件上传从桌面拖动的文件?)
Drop image into contenteditable in Chrome to the cursor(将图像拖放到 Chrome 中的 contenteditable 到光标处)