本文整理汇总了TypeScript中@angular/http/testing.MockBackend类的典型用法代码示例。如果您正苦于以下问题:TypeScript MockBackend类的具体用法?TypeScript MockBackend怎么用?TypeScript MockBackend使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MockBackend类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: beforeEach
beforeEach(inject([MockBackend], (backend: MockBackend) => {
const baseResponse = new Response(new ResponseOptions({ body: 'got response' }));
backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse));
backend.createConnection(new Request({
method: RequestMethod.Get,
url: 'http://localhost/Nana10MVC/service/126',
search: 'password=123'
}));
}));
示例2: it
it('should get techs', async(inject([MockBackend, Techs], (mockBackend: MockBackend, techs: Techs) => {
let conn: MockConnection;
const response = new Response(new ResponseOptions({body: techsJson}));
mockBackend.connections.subscribe((connection: MockConnection) => {
conn = connection;
});
techs.getTechs().subscribe((jsonObject => {
techs.techs = jsonObject;
}));
conn.mockRespond(response);
expect(techs.techs.length).toBe(3);
mockBackend.verifyNoPendingRequests();
})));
示例3: it
it('should get application data and cache it', async(inject([NavService, XHRBackend], (service: NavService, backend: MockBackend) => {
let count = 0;
backend.connections.subscribe((connection: MockConnection) => {
expect(connection.request.method).toBe(RequestMethod.Get);
expect(connection.request.url).toBe('http://localhost:8080/application');
connection.mockRespond(new Response(new ResponseOptions({body: '{"expects": "JSON"}', status: 200})));
count++;
});
service.getNavData().subscribe((data: any) => {
expect(data.expects).toEqual('JSON');
});
service.getNavData().subscribe((data: any) => {
expect(data.expects).toEqual('JSON');
});
expect(backend.connectionsArray.length).toBe(1);
backend.verifyNoPendingRequests();
})));
示例4: describe
describe('ItemsService', () => {
let http: Http,
injector: Injector,
backend: MockBackend,
service: ItemsService,
setConnection: Function;
beforeEach(() => {
injector = ReflectiveInjector.resolveAndCreate([
ItemsService, BaseRequestOptions, MockBackend, {
provide: Http,
useFactory: function(backend: ConnectionBackend, defaultOptions: BaseRequestOptions) {
return new Http(backend, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]
}
]);
http = injector.get(Http);
backend = injector.get(MockBackend);
service = injector.get(ItemsService);
setConnection = (options): void => {
const responseOptions = { body: options},
baseResponse = new Response(new ResponseOptions(responseOptions));
backend.connections.subscribe((connection: MockConnection) => {
connection.mockRespond(baseResponse);
});
};
});
afterEach(() => backend.verifyNoPendingRequests());
it('#loadItems', () => {
const requestBody = {id: 1, name: 'First Item'};
setConnection(requestBody);
spyOn(http, 'get').and.callThrough();
service.loadItems()
.then((res) => {
expect(http.get).toHaveBeenCalled();
expect(res).toEqual(requestBody);
});
});
it('#saveItem', () => {
const newItem = { id: undefined, name: 'New Item', description: 'Description' },
existingItem = { id: 1, name: 'Existing Item', description: 'Description' };
const createItem = spyOn(service, 'createItem').and.callThrough();
const updateItem = spyOn(service, 'updateItem');
service.saveItem(newItem);
expect(service.createItem).toHaveBeenCalled();
expect(service.updateItem).not.toHaveBeenCalled();
service.saveItem(existingItem);
expect(createItem.calls.count()).toEqual(1);
expect(updateItem).toHaveBeenCalled();
});
it('#createItem', () => {
const requestBody = { id: 1, name: 'First Item', description: 'Described' };
setConnection(requestBody);
const post = spyOn(http, 'post').and.callThrough();
service.createItem(requestBody)
.then((res) => {
expect(post.calls.argsFor(0).length).toBe(3);
expect(res).toEqual(requestBody);
});
});
it('#updateItem', () => {
const requestBody = { id: 1, name: 'First Item Updated', description: 'Described' };
setConnection(requestBody);
const put = spyOn(http, 'put').and.callThrough();
service.updateItem(requestBody)
.then((res) => {
expect(put.calls.argsFor(0).length).toBe(3);
expect(res).toEqual(requestBody);
});
});
it('#deleteItem', () => {
const requestBody = { id: 1, name: 'First Item Deleted', description: 'Described' };
setConnection(requestBody);
const del = spyOn(http, 'delete').and.callThrough();
service.deleteItem(requestBody)
.then((res) => {
expect(del.calls.argsFor(0).length).toBe(1);
//.........这里部分代码省略.........
示例5: describe
describe("Hero Integration tests", () => {
let service: HeroService;
let configService: SdkSampleConfigService;
let mockBackend: MockBackend;
beforeEachProviders(() => [
HTTP_PROVIDERS,
GLOBAL_PROVIDERS,
COMMON_PROVIDERS,
provide(XHRBackend, { useClass: MockBackend }),
SdkSampleConfigService,
HeroService,
HeroClient
]);
beforeEach(inject([
XHRBackend,
HeroService,
SdkSampleConfigService,
LoggingService,
CommonHttpClient
], (
_mockBackend: MockBackend,
_service: HeroService,
_configService: SdkSampleConfigService,
_loggingService: LoggingService,
_commonHttpClient: CommonHttpClient
) => {
service = _service;
configService = _configService;
mockBackend = _mockBackend;
spyOn(_loggingService, "log").and.stub();
configService.set({
baseUri: "http://obg-sdk.test.com"
});
utils.httpMock.mockResponse(mockBackend, {
body: heroes,
status: statusCodes.OK
});
_commonHttpClient.initialize({
baseUri: "http://obg-sdk.test.com",
brandId: "b0032790-0fc8-4e38-b98f-71df0a4f6753",
marketCode: "en"
});
}));
afterEach(() => {
mockBackend.resolveAllConnections();
mockBackend.verifyNoPendingRequests();
});
describe("when getAll is invoked", () => {
it("should return all items", (done: Function) => {
service.getAll()
.subscribe(x => {
expect(x.length).toBe(5);
done();
});
});
});
});
示例6: describe
describe('QuoteService', () => {
let quoteService: QuoteService;
let mockBackend: MockBackend;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
QuoteService,
MockBackend,
BaseRequestOptions,
{
provide: Http,
useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backend, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]
}
]
});
});
beforeEach(inject([
QuoteService,
MockBackend
], (_quoteService: QuoteService,
_mockBackend: MockBackend) => {
quoteService = _quoteService;
mockBackend = _mockBackend;
}));
afterEach(() => {
mockBackend.verifyNoPendingRequests();
});
describe('getRandomQuote', () => {
it('should return a random Chuck Norris quote', fakeAsync(() => {
// Arrange
const mockQuote = 'a random quote';
const response = new Response(new ResponseOptions({
body: { value: mockQuote }
}));
mockBackend.connections.subscribe((connection: MockConnection) => connection.mockRespond(response));
// Act
const randomQuoteSubscription = quoteService.getRandomQuote({ category: 'toto' });
tick();
// Assert
randomQuoteSubscription.subscribe((quote: string) => {
expect(quote).toEqual(mockQuote);
});
}));
it('should return a string in case of error', fakeAsync(() => {
// Arrange
const response = new Response(new ResponseOptions({ status: 500 }));
mockBackend.connections.subscribe((connection: MockConnection) => connection.mockError(response as any));
// Act
const randomQuoteSubscription = quoteService.getRandomQuote({ category: 'toto' });
tick();
// Assert
randomQuoteSubscription.subscribe((quote: string) => {
expect(typeof quote).toEqual('string');
expect(quote).toContain('Error');
});
}));
});
});
示例7: afterEach
afterEach(() => backend.verifyNoPendingRequests());
示例8:
afterEach(() => {
mockBackend.resolveAllConnections();
mockBackend.verifyNoPendingRequests();
});
示例9: afterEach
afterEach(() => {
mockBackend.verifyNoPendingRequests();
});