Unit testing 如何在从另一个服务继承的服务中模拟/伪造一个方法,并返回一个可观察的方法?

Unit testing 如何在从另一个服务继承的服务中模拟/伪造一个方法,并返回一个可观察的方法?,unit-testing,jasmine,angular-unit-test,Unit Testing,Jasmine,Angular Unit Test,我想为服务中的方法编写一个单元测试。但是,该方法是在另一个服务中编写的,在这里使用。我想模仿/假装所说的方法 这是服务: export class UserService extends ProxyService<UserInterface> { constructor() { super(new User()); } getOneByEmail(email: string): Observable<any> { return this.get

我想为服务中的方法编写一个单元测试。但是,该方法是在另一个服务中编写的,在这里使用。我想模仿/假装所说的方法

这是服务:

export class UserService extends ProxyService<UserInterface> {
  constructor() {
    super(new User());
  }
  getOneByEmail(email: string): Observable<any> {
    return this.getUri('user/by-email/').pipe(map((result: any) => result));
  }
  getCurrentUser(): UserInterface {
    return new User().init(localStorage.getItem('user'));
  }
}
前两个测试运行良好。 在subscribe方法中,未识别任何期望

getUri(uri: string): Observable<any> {
    return this.remoteStorageService.getUri(uri).pipe(map((result: any) => result));
  }
describe('UserService', () => {
  const data = {
    email: 'test@gmail.com',
  };
  const fakeUser = new User().init(data);
  beforeAll(() => {
    TestBed.initTestEnvironment(
      BrowserDynamicTestingModule,
      platformBrowserDynamicTesting()
    );
  });
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        // angular testing module that provides mocking for http connections
        HttpClientTestingModule,
      ],
      // add declaration of services or components and use inject to get to them in tests
      providers: [
        Injector,
        {
          provide: UserService,
          useValue: new User()
        },
      ],
    });
  }));
  beforeEach(() => {
    AppModule.InjectorInstance = TestBed;
    TestBed.inject(UserService);
  });
  it('should be created', () => {
    const service: UserService = TestBed.inject(UserService);
    expect(service).toBeTruthy();
  });
  it('getCurrentUser should return a user', () => {
    const service: UserService = new UserService();
    const user = new User().init();
    expect(service.getCurrentUser()).toEqual(user);
  });
  it('getOneByEmail should return the user who has the specified email', fakeAsync(inject([
    HttpTestingController,
    UserService,
  ], (
    httpMock: HttpTestingController,
    done
  ) => {
    const service = new UserService();
    service.getOneByEmail(data.email).subscribe((res) => {
      expect(res.data.length).toBe(1);
      expect(res.data[0]).toBeInstanceOf(User);
      done();
    });
    const req = httpMock.expectOne('http://projekt.test/by-email/');
    expect(req.request.method).toEqual('GET');
  }
  )));
});