本文整理匯總了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;
})));