Skip to content

Use $httpBackend service in unit tests with apiMock #64

@juri33

Description

@juri33

Your package is really helpful. Thank you guys.

I my old unit tests i am using $httpBackend with .whenGET mocks.

beforeEach(() => {
  inject(function ($injector, _$compile_, _$rootScope_) {
    // The injector unwraps the underscores (_) from around the parameter names when matching
    $compile = _$compile_;
    $rootScope = _$rootScope_;
    $httpBackend = $injector.get("$httpBackend");
  });

$httpBackend.whenGET("api/langs/gb.json").respond({ "COMMON.HOME": homeName });
$httpBackend.whenGET("api/langs/de.json").respond({});

$httpBackend.whenGET(/^private\/auth\?.*/).respond({});

directiveElem = getCompiledElement();
});

Now i have included apiMock and my unit tests doesn't work any more.

it("should be defined and contiain first list item for home.", function () {

$httpBackend.flush();
expect(directiveElem).toBeDefined();

let olElement = directiveElem.find("ol");
expect(olElement).toBeDefined();

let homeElement = directiveElem.find("li");
expect(homeElement.html()).toContain(homeName);
});

I have tried

  apiMockProvider.config({
    disable: true
  });

but it didn't help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions