更改代码后HttpClientTestingModule不起作用

时间:2019-05-07 17:30:11

标签: javascript angular

当我进行了一些代码更改时,我正在修改我现有的工作示例。我已经放弃了该服务,并且正在从效果中发出http请求。

下面是测试用例,代码也可以正常运行,这是我目前试图对其进行修改的方式。感谢您的输入!

//工作效果。ts

@Injectable()
export class ContactInfoEffect {
private url = environment.urls.contactInfo;
@Effect()
fetchContactInfoResponse = this.actions.pipe(
ofType(ContactInfoRequested.type),
mergeMap(action =>
  this.contactTriageService.getContactTriageResponse(this.url).pipe(
    map((contacts: ContactInfoResponse) => 
ContactInfoSucceeded(contacts)),
     catchError(() => of(ContactInfoFailed()))
  )
 )
 );

constructor(
private actions: Actions,
private contactTriageService: ContactTriageService
) {}
}

//这是有效的测试用例

describe('ContactInfoEffect', () => {
let actions: Subject<any>;
const spectator = createService<ContactInfoEffect>({
service: ContactInfoEffect,
imports: [HttpClientTestingModule],
providers: [provideMockActions(() => actions)],
mocks: [ContactTriageService]
});

it('should dispatch ContactInfoSucceeded if the service returns 
successfully', () => {
const response: ContactInfoResponse = getContactInfoMock();
actions = new ReplaySubject(1);
spectator
  .get<ContactTriageService>(ContactTriageService)
  .getContactTriageResponse.and.returnValue(of(response));
actions.next( ContactInfoRequested());
spectator.service.fetchContactInfoResponse.subscribe(result => {
  expect(result).toEqual( ContactInfoSucceeded(response));
});
});

///现在,我已经编辑好effects.ts以直接使用http:

@Injectable()
export class ContactInfoEffect {
private url = environment.urls.contactInfo;
@Effect()
fetchContactInfoResponse = this.actions.pipe(
ofType(contactInfoRequested.type),
mergeMap(action =>
  this.http.get<ContactInfoResponse>(this.url).pipe(
    map((contacts: ContactInfoResponse) => 
contactInfoSucceeded(contacts)),
    catchError(() => of(contactInfoFailed()))
  )
)
);

constructor(
private actions: Actions,
private http: HttpClient
) {}
}

//我修改它的方式实际上不起作用:

describe('ContactInfoEffect', () => {
let injector: Injector;
let httpMock: HttpTestingController;
let actions: Subject<any>;
const spectator = createService<ContactInfoEffect>({
service: ContactInfoEffect,
imports: [HttpClientTestingModule],
providers: [provideMockActions(() => actions)],
mocks: [HttpClientTestingModule]
});
it('should dispatch ContactInfoSucceeded if the service returns 
successfully', () => {
const response: ContactInfoResponse = getContactInfoMock();
actions = new ReplaySubject(1);
spectator
  .get<any>(httpMock).and.returnValue(of(response));
actions.next(contactInfoRequested());
spectator.service.fetchContactInfoResponse.subscribe(result => {
  expect(result).toEqual(contactInfoSucceeded(response));
});
});

0 个答案:

没有答案