2014-02-19 66 views
1

我有一個測試(從AngularDart項目中使用茉莉語法)MockHttpBackend - 意外的請求

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'); 
    }))); 

導致

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) 

任何想法,我做錯了嗎?

+0

看起來http請求不使用相同的HttpBackend我注入,但爲什麼? –

回答

2

看那個代碼片段:

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'); 
    }); 
    }); 
} 

那是你在找什麼?

Regards, Sergey。

+0

愚蠢的我,它也適用於注入'HttpBackend'。但是,當我註冊'm.type(HttpBackend後端:實現:MockHttpBackend);''我必須像使用它注入(HttpBackend後端){(後端爲MockHttpBackend).expect('GET','/url').respond ('X'); });'和** not **'注入(** Mock ** HttpBackend ...'。 感謝Sergey提供的支持! –

相關問題