seriema / angular-apimock

Automatically route your API calls to static JSON files, for hiccup free front–end development.
johansson.jp/angular-apimock
MIT License
65 stars 8 forks source link

Use $httpBackend service in unit tests with apiMock #64

Open juri33 opened 7 years ago

juri33 commented 7 years ago

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.

seriema commented 7 years ago

Hmm odd, I use $httpBackend in our unit tests. The disable flag would still keep apiMock registered, just not run any logic. Still odd that it's breaking for you. What error are you seeing @juri33?