茉莉花angularjs - 刺探的方法时控制器初始化时调用 [英] Jasmine angularjs - spying on a method that is called when controller is initialized

查看:94
本文介绍了茉莉花angularjs - 刺探的方法时控制器初始化时调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前用茉莉花与噶(Testacular)和Web风暴编写单元测试。我有麻烦刺探一个当控制器初始化该被立即调用方法。是否有可能在方法窥探当控制器初始化时调用?

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?

我的控制器code,我试图窥探是方法 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';
});

这里是测试规范:

And here is the test spec:

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();
    }));


});
});

该测试失败说,该方法没有被调用。我知道我应该嘲笑 DataService的并传递到测试控制器。但是好像我还是会对这个方法刺探无论是模拟还是不能当同样的问题。任何人有任何意见或可以点我的资源在处理这个正确的方式?

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?

推荐答案

在编写单元测试,你应该每件code的隔离。在这种情况下,你需要隔离你的服务,并分别对其进行测试。创建服务的模拟,并把它传递给控制器​​。

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.

这篇关于茉莉花angularjs - 刺探的方法时控制器初始化时调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆