1

I have a test (using jasmine syntax from the AngularDart project)

describe("MockHttpBackend", () {

  beforeEach(() {
    setUpInjector();
    module((Module module) {
      module
      ..type(HttpBackend, implementedBy: MockHttpBackend);
      // ..value(HttpBackend, new MockHttpBackend()); // same problem
    });
  });

  it('should do basic request', async(inject((Http http, MockHttpBackend backend) {
    backend.expect('GET', '/url').respond('');
    http(url: '/url', method: 'GET');
  })));

which results in

Test failed: Caught [Async error, [Unexpected request: GET /url No more requests expected], 
#0 > MockHttpBackend.call (package:angular/mock/http_backend.dart:224:5) 
#1 MockHttpBackend.request (package:angular/mock/http_backend.dart:137:9)

Any idea what I'm doing wrong?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567

1 Answers1

2

Look at that code snippet:

void mockTests() {
  describe("MockHttpBackend", () {

    TestBed _;
    Scope scope;
    Http http;
    MockHttpBackend backend;

    beforeEach(setUpInjector);
    beforeEach(module((Module m) {
      m.type(HttpBackend, implementedBy:MockHttpBackend);
    }));
    beforeEach(inject((TestBed tb) => _ = tb));
    beforeEach(inject((Scope s) => scope = s));
    beforeEach(inject((Http h) => http = h));
    beforeEach(inject((HttpBackend h) => backend = h));


    it('should do basic request', () {
      backend.expect('GET', '/url').respond('');
      http(url: '/url', method: 'GET');
    });
  });
}

Is that what are you looking for?

Regards, Sergey.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
akserg
  • 436
  • 2
  • 4
  • Stupid me, it works also with injected `HttpBackend`. But when I register `m.type(HttpBackend backend: implementedBy: MockHttpBackend);` I have to use it like `inject(HttpBackend backend) { (backend as MockHttpBackend).expect('GET', '/url').respond('x'); });` and **not** 'inject( **Mock** HttpBackend ...'. Thanks Sergey for your support! – Günter Zöchbauer Feb 19 '14 at 18:14