當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript MockConnection.mockError方法代碼示例

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

        }
    }
開發者ID:feibeck,項目名稱:fussi,代碼行數:32,代碼來源:player-api.ts

示例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);
開發者ID:jaffbeto,項目名稱:angular-login,代碼行數:28,代碼來源:fake-backend.ts

示例3: Error

 mockBackend.connections.subscribe((c: MockConnection) => {
     if (responseMessageSuccess) {
         c.mockRespond(userResponse);
     } else {
         c.mockError(new Error('login failed'));
     }
 });
開發者ID:iraghumitra,項目名稱:incubator-metron,代碼行數:7,代碼來源:authentication.service.spec.ts

示例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);
 });
開發者ID:gabyvs,項目名稱:ng2-ue-utils,代碼行數:25,代碼來源:client.spec.ts

示例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"));

    });
開發者ID:training4developers,項目名稱:ng2-widgets-app,代碼行數:9,代碼來源:widgets.service.spec.ts

示例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']);
    });
開發者ID:kensodemann,項目名稱:time-trax,代碼行數:14,代碼來源:time-trax-http.service.spec.ts

示例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");
    }));
開發者ID:Toolwatchapp,項目名稱:tw-common,代碼行數:15,代碼來源:twapi.service.spec.ts

示例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;

  })));
開發者ID:ubiquits,項目名稱:ubiquits,代碼行數:17,代碼來源:http.store.spec.ts


注:本文中的@angular/http/testing.MockConnection.mockError方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。