2017-05-19 26 views
0

にアップグレードした後、私はエラー [ここに画像の説明を入力します]の下に取得しています[1]型キャストの問題角度4にアップグレードした後の角4

クライアントafea:?119 [アット・ローダー] ./src/test /javascript/spec/app/account/settings/settings.component.spec.ts:49:13 TS2322: 'Principal'タイプは 'MockPrincipal'タイプに割り当てられません。 プロパティ 'identitySpy'が 'プリンシパル'タイプにありません。

[AT-ローダ] ./src/test/javascript/spec/app/account/settings/settings.component.spec.ts:48:13 TS2322:タイプ 'AccountServiceの'「はMockAccountServiceを入力する割り当て可能ではありません' プロパティ 'getSpy'が 'AccountService'タイプにありません。

settings.component.spec.ts

import { ComponentFixture, TestBed, async } from '@angular/core/testing'; 
import { Observable } from 'rxjs/Rx'; 
import { JhiLanguageHelper } from '../../../../../../main/webapp/app/shared'; 
import { AgreeGatewayTestModule } from '../../../test.module'; 
import { Principal, AccountService } from '../../../../../../main/webapp/app/shared'; 
import { SettingsComponent } from '../../../../../../main/webapp/app/account/settings/settings.component'; 
import { MockAccountService } from '../../../helpers/mock-account.service'; 
import { MockPrincipal } from '../../../helpers/mock-principal.service'; 


describe('Component Tests',() => { 

    describe('SettingsComponent',() => { 

     let comp: SettingsComponent; 
     let fixture: ComponentFixture<SettingsComponent>; 
     let mockAuth: MockAccountService; 
     let mockPrincipal: MockPrincipal; 

     beforeEach(async(() => { 
      TestBed.configureTestingModule({ 
       imports: [AgreeGatewayTestModule], 
       declarations: [SettingsComponent], 
       providers: [ 
        { 
         provide: Principal, 
         useClass: MockPrincipal 
        }, 
        { 
         provide: AccountService, 
         useClass: MockAccountService 
        }, 
        { 
         provide: JhiLanguageHelper, 
         useValue: null 
        }, 
       ] 
      }).overrideComponent(SettingsComponent, { 
       set: { 
        template: '' 
       } 
      }).compileComponents(); 
     })); 

     beforeEach(() => { 
      fixture = TestBed.createComponent(SettingsComponent); 
      comp = fixture.componentInstance; 
      console.log(AccountService); 
      mockAuth = fixture.debugElement.injector.get(AccountService); 
      mockPrincipal = fixture.debugElement.injector.get(Principal); 
     }); 

     it('should send the current identity upon save', function() { 
      // GIVEN 
      let accountValues = { 
       firstName: 'John', 
       lastName: 'Doe', 

       activated: true, 
       email: '[email protected]', 
       langKey: 'en', 
       login: 'john' 
      }; 
      mockPrincipal.setResponse(accountValues); 

      // WHEN 
      comp.settingsAccount = accountValues; 
      comp.save(); 

      // THEN 
      expect(mockPrincipal.identitySpy).toHaveBeenCalled(); 
      expect(mockAuth.saveSpy).toHaveBeenCalledWith(accountValues); 
      expect(comp.settingsAccount).toEqual(accountValues); 
     }); 

     it('should notify of success upon successful save', function() { 
      // GIVEN 
      let accountValues = { 
       firstName: 'John', 
       lastName: 'Doe' 
      }; 
      mockPrincipal.setResponse(accountValues); 

      // WHEN 
      comp.save(); 

      // THEN 
      expect(comp.error).toBeNull(); 
      expect(comp.success).toBe('OK'); 
     }); 

     it('should notify of error upon failed save', function() { 
      // GIVEN 
      mockAuth.saveSpy.and.returnValue(Observable.throw('ERROR')); 

      // WHEN 
      comp.save(); 

      // THEN 
      expect(comp.error).toEqual('ERROR'); 
      expect(comp.success).toBeNull(); 
     }); 
    }); 
}); 

account.service.ts

import { Injectable } from '@angular/core'; 
import { Http, Response } from '@angular/http'; 
import { Observable } from 'rxjs/Rx'; 

@Injectable() 
export class AccountService { 
    constructor(private http: Http) { } 

    get(): Observable<any> { 
     return this.http.get('api/account').map((res: Response) => res.json()); 
    } 

    save(account: any): Observable<Response> { 
     return this.http.post('api/account', account); 
    } 
} 

モックaccount.service.ts

import { SpyObject } from './spyobject'; 
import { AccountService } from '../../../../main/webapp/app/shared/auth/account.service'; 
import Spy = jasmine.Spy; 

export class MockAccountService extends SpyObject { 

    getSpy: Spy; 
    saveSpy: Spy; 
    fakeResponse: any; 

    constructor() { 
     super(AccountService); 

     this.fakeResponse = null; 
     this.getSpy = this.spy('get').andReturn(this); 
     this.saveSpy = this.spy('save').andReturn(this); 
    } 

    subscribe(callback: any) { 
     callback(this.fakeResponse); 
    } 

    setResponse(json: any): void { 
     this.fakeResponse = json; 
    } 
} 

モックprincipal.service.ts

import { SpyObject } from './spyobject'; 
import { Principal } from '../../../../main/webapp/app/shared/auth/principal.service'; 
import Spy = jasmine.Spy; 

export class MockPrincipal extends SpyObject { 

    identitySpy: Spy; 
    fakeResponse: any; 

    constructor() { 
     super(Principal); 

     this.fakeResponse = {}; 
     this.identitySpy = this.spy('identity').andReturn(Promise.resolve(this.fakeResponse)); 
    } 

    setResponse(json: any): void { 
     this.fakeResponse = json; 
    } 
} 
+0

は、あなたが投稿することができて、あなたのモックオブジェクトの拡張子を変更しようとすることができあなたのコード?私の推測では、あなたが正しい方法を継承しないか、または継承を欠いているということです。 – John

+0

ちょっと分私のコードを共有します –

+0

私はテストに慣れていないので、実際にはわかりませんが、モッククラスを拡張しましたか? が好きです: 'class MockPrincipal extends Principal {}'あなたのモッククラスも提供できますか? – John

答えて

0

私はテストとそれほど慣れていないよ、私は本当に知らないが、私はあなたのテストベッドで正しいサービスを提供しなければならないと思います。あなたは型の競合を起こさないためにプロバイダを拡張する必要があります。それは、ない作業を行う場合

beforeEach(async(() => { 
      TestBed.configureTestingModule({ 
       imports: [AgreeGatewayTestModule], 
       declarations: [SettingsComponent], 
       providers: [ 
        { 
         provide: SpyObject, //changed this 
         useClass: MockPrincipal 
        }, 
        { 
         provide: SpyObject, //changed this 
         useClass: MockAccountService 
        }, 
        { 
         provide: JhiLanguageHelper, 
         useValue: null 
        }, 
       ] 
      }).overrideComponent(SettingsComponent, { 
       set: { 
        template: '' 
       } 
      }).compileComponents(); 
     })); 

、あなたの代わりにPrincipalAccountService

export class MockPrincipal extends Principal { 

    identitySpy: Spy; 
    fakeResponse: any; 

    constructor() { 
     super(Principal); 

     this.fakeResponse = {}; 
     this.identitySpy = this.spy('identity').andReturn(Promise.resolve(this.fakeResponse)); 
    } 

    setResponse(json: any): void { 
     this.fakeResponse = json; 
    } 
} 

export class MockAccountService extends AccountService { 

    getSpy: Spy; 
    saveSpy: Spy; 
    fakeResponse: any; 

    constructor() { 
     super(AccountService); 

     this.fakeResponse = null; 
     this.getSpy = this.spy('get').andReturn(this); 
     this.saveSpy = this.spy('save').andReturn(this); 
    } 

    subscribe(callback: any) { 
     callback(this.fakeResponse); 
    } 

    setResponse(json: any): void { 
     this.fakeResponse = json; 
    } 
}