本文整理汇总了TypeScript中@angular/http/testing.MockConnection.mockError方法的典型用法代码示例。如果您正苦于以下问题:TypeScript MockConnection.mockError方法的具体用法?TypeScript MockConnection.mockError怎么用?TypeScript MockConnection.mockError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类@angular/http/testing.MockConnection
的用法示例。
在下文中一共展示了MockConnection.mockError方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: update
public update(connection: MockConnection) {
const requestPlayer = JSON.parse(connection.request.getBody());
const validationResult = this.playerDb.validate(requestPlayer);
if (validationResult === true) {
let updatedPlayer = this.playerDb.save(requestPlayer);
if (updatedPlayer === false) {
connection.mockError(new ErrorResponse(new ResponseOptions({
body: 'Player not found',
status: 404
})));
} else {
connection.mockRespond(new Response(new ResponseOptions({
body: JSON.stringify(updatedPlayer)
})));
}
} else {
connection.mockError(new ErrorResponse(new ResponseOptions({
body: JSON.stringify({errors: validationResult}),
status: 422
})));
}
}
示例2: setTimeout
setTimeout(() => {
// fake authenticate api end point
if (connection.request.url.endsWith('/api/authenticate/login') && connection.request.method === RequestMethod.Post) {
let params = JSON.parse(connection.request.getBody());
// check user credentials and return fake jwt token if valid
let found: User = USERS.find((user: User) => {return (params.username === user.username);});
if (found) {
if(params.password === found.password) {
connection.mockRespond(new Response(
new ResponseOptions({status: 200, body: {token: 'fake-token-jwt', user: found}})
));
}else{
connection.mockError(new MockError(new ResponseOptions({type:ResponseType.Error, status:400, body: JSON.stringify({code: 2, message: 'The password does not match '})})));
}
} else {
connection.mockError(new MockError(new ResponseOptions({type:ResponseType.Error, status:400, body: JSON.stringify({code: 1, message: 'Username does not exists'})})));
}
}
if (connection.request.url.endsWith('/api/authenticate/logout') && connection.request.method === RequestMethod.Post) {
let params = JSON.parse(connection.request.getBody());
connection.mockRespond(new Response(
new ResponseOptions({status: 200, body: true})
));
}
}, 100);
示例3: Error
mockBackend.connections.subscribe((c: MockConnection) => {
if (responseMessageSuccess) {
c.mockRespond(userResponse);
} else {
c.mockError(new Error('login failed'));
}
});
示例4: it
it('GET with malformed error', (done) => {
let connection: MockConnection;
backend.connections.subscribe((cn: MockConnection) => connection = cn);
client.get(someUrl).subscribe(
next => {
expect('Should not enter success case').toBeUndefined();
done();
},
error => {
expect(error).toBeDefined();
expect(error.message).toBeDefined();
expect(error.message).toBe('Internal Server Error');
expect(error.code).toBe('500');
const theMalFormedError: IGAEvent = window.dataLayer[0];
expect(theMalFormedError.event).toBe('Edge_malFormedError');
expect(theMalFormedError.target).toBe('ProductsSPA');
expect(theMalFormedError.action).toBe(someUrl);
expect(theMalFormedError['target-properties']).toBe('{"context":[]}');
done();
}
);
const err: any = newMalFormedError();
err.name = 'error name';
connection.mockError(err);
});
示例5:
mockBackend.connections.subscribe((connection: MockConnection) => {
if(connection.request.url.endsWith("/widgets")) {
connection.mockRespond(mockResponse);
}
connection.mockError(Error("request url did not match expected url"));
});
示例6: it
it('redirects to login on a 401 error', () => {
let connection: MockConnection;
token = null;
spyOn(mockRouter, 'navigate');
mockBackend.connections.subscribe(c => connection = c);
service.get('http://test.dr.who/companions').catch(res => Observable.empty()).subscribe();
const response = new Response(new ResponseOptions({
status: 401
}));
connection.mockError(response as any as Error);
expect(mockRouter.navigate).toHaveBeenCalledTimes(1);
expect(mockRouter.navigate).toHaveBeenCalledWith(['authentication', 'login']);
});
示例7: it
it('shouldn\'t log an user (bad cred + bad error)', fakeAsync(() => {
var user: User;
let error;
twAPIService.login("m@m.com", "qwerty").then(
response => { user = response; },
reject => { error = reject; }
);
lastConnection.mockError(new Error());
tick();
expect(user).toBeUndefined();
expect(error).toBeDefined();
expect(lastConnection.request.url).toEqual(twAPIService.config.getAPIUrl() + "users", "should be consumed");
}));
示例8: it
it('Logs error on failed model retrieval', async(inject([TestHttpStore, MockBackend], (s: TestHttpStore, b: MockBackend) => {
let logSpy = spyOn((s as any).logger, 'error');
let connection: MockConnection;
b.connections.subscribe((c: MockConnection) => connection = c);
const testPromise = s.findOne(null)
.catch((res) => {
expect(logSpy)
.toHaveBeenCalledWith('Internal Error');
});
connection.mockError(new Error('Internal Error'));
return testPromise;
})));