How to reuse common angular unit test functions

I find that when writing angular unit tests, I often do similar things in every test file, for example. compiling and digesting directives. I would like to bring these functions to a separate library, but I wonder how best to do this.

I looked at angular.mocks and saw that it uses a module template to add angular.mocks to the window, and I load angular-mocks.js through an array of karma files. I have no problem creating my library, but I was wondering if there was any request ("mylib") that I could use in my test, which I did not know about.

+4
source share
1 answer

$controller . .

helper.js

var Helper = function($httpBackend, customParam) {
    this.$httpBackend = $httpBackend;
    this.customParam = customParam;
}

Helper.prototype.mockHttpRequest = function() {
    this.$httpBackend.when(...);
}

test.js

it('should do something', inject(function($controller) {
    var helper = $controller('Helper', {customParam: 123});

    helper.mockHttpRequest();

    expect(...);
}));
0

Source: https://habr.com/ru/post/1546993/


All Articles