Picci
Picci

Reputation: 17752

Testing Angular Http services via Jasmine / Karma

For a series of reasons I would like to be able to test some Http services (not via mock) using Jasmine / Karma support of Angualar.

If I use the following code

beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [],
      imports: [
        HttpModule
      ]
    });
    .......

tests fail with the following message printed on Karma browser

Error: No provider for Http!
Error: DI Error
    at NoProviderError.ZoneAwareError (http://localhost:9876/base/src/polyfills.ts?1adeefc4ae1dd1c3f12dac836469652bbacab66c:2054:33)
    at NoProviderError.BaseError [as constructor] (http://localhost:9876/base/src/test.ts?5cfdb8c92873c902e956a345863bab75c6324f3e:23292:16)
    at NoProviderError.AbstractProviderError [as constructor] (http://localhost:9876/base/src/test.ts?5cfdb8c92873c902e956a345863bab75c6324f3e:47503:16)
    at new NoProviderError (http://localhost:9876/base/src/test.ts?5cfdb8c92873c902e956a345863bab75c6324f3e:47565:16)
    at ReflectiveInjector_._throwOrNull (http://localhost:9876/base/src/test.ts?5cfdb8c92873c902e956a345863bab75c6324f3e:63961:19)
.......

Any suggestion on how to fix it would be very much appreciated

Upvotes: 1

Views: 173

Answers (1)

Will
Will

Reputation: 7017

You may need to configure the provider for Http. For example:

TestBed.configureTestingModule({
  providers: [ { provide: Http, useClass: Http } ],

If that doesn't do the trick, please share your imports and any earlier code in the test spec.

Upvotes: 1

Related Questions