spyOn 无法监视依赖服务的方法
spyOn not able to spy on the method of a dependent service
我正在测试 AuthService
,它使用另一个 HelperService
.
将用户登录信息发送到服务器
public authServiceSigninUser(user:UserSigninInfo):any{
console.log('In authServiceSigninUser. contacting server at '+this.API_URL +this.SIGNIN_USER_URL +" with user data "+user+ " with httpOptions "+httpOptions.withCredentials + ","+httpOptions.headers ); //TODOM password should be sent in encrypted format.
let signinInfo= new UserSigninAPI(user);
let body = JSON.stringify(signinInfo);
return this.helperService.sendMessage(this.SIGNIN_USER_URL,body,httpOptions)
}
我正在尝试按如下方式测试 authServiceSigninUser
方法,但是当我 运行 规范时,出现错误 Error: <spyOn> : sendMessage() method does not exist
Usage: spyOn(<object>, <methodName>)
。为什么?
describe('authServiceSigninUser test suite',()=>{
beforeEach(()=>{
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [AuthService, HelperService]
});
});
fit('should sign in user',()=>{
let spy:any;
let helper = TestBed.get(HelperService);
let authService = TestBed.get(AuthService);
let userSignIn = new UserSigninInfo("test@test.com","test");
let httpMock = TestBed.get(HttpTestingController);
spyOn(helper.sendMessage,'sendMessage');
let observable:Observable<HttpEvent<any>> = authService.authServiceSigninUser(userSignIn);
let subscription = observable.subscribe((event)=>{
console.log('event from authService',event);
});
const responseData = { result: 'success', ['additional-info']: 'login success' };
let httpEvent:HttpResponse<any> = new HttpResponse<any>({body:responseData});
expect(helper.sendMessage).toHaveBeenCalled();//ERROR here
const mockReq:TestRequest = httpMock.expectOne(environment.apiUrl+environment.signinUserUrl); //Expect that a single request has been made which matches the given URL, and return its mock
//once mocking of sending request is done, mock receiving a response. This will trigger the logic inside subscribe function
mockReq.flush(httpEvent); //flush method provides dummy values as response
httpMock.verify();//verify checks that there are no outstanding requests;
});
});
看来正确的方法是spyOn(helper,'sendMessage');
,而不是spyOn(helper.sendMessage,'sendMessage');
我正在测试 AuthService
,它使用另一个 HelperService
.
public authServiceSigninUser(user:UserSigninInfo):any{
console.log('In authServiceSigninUser. contacting server at '+this.API_URL +this.SIGNIN_USER_URL +" with user data "+user+ " with httpOptions "+httpOptions.withCredentials + ","+httpOptions.headers ); //TODOM password should be sent in encrypted format.
let signinInfo= new UserSigninAPI(user);
let body = JSON.stringify(signinInfo);
return this.helperService.sendMessage(this.SIGNIN_USER_URL,body,httpOptions)
}
我正在尝试按如下方式测试 authServiceSigninUser
方法,但是当我 运行 规范时,出现错误 Error: <spyOn> : sendMessage() method does not exist
Usage: spyOn(<object>, <methodName>)
。为什么?
describe('authServiceSigninUser test suite',()=>{
beforeEach(()=>{
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [AuthService, HelperService]
});
});
fit('should sign in user',()=>{
let spy:any;
let helper = TestBed.get(HelperService);
let authService = TestBed.get(AuthService);
let userSignIn = new UserSigninInfo("test@test.com","test");
let httpMock = TestBed.get(HttpTestingController);
spyOn(helper.sendMessage,'sendMessage');
let observable:Observable<HttpEvent<any>> = authService.authServiceSigninUser(userSignIn);
let subscription = observable.subscribe((event)=>{
console.log('event from authService',event);
});
const responseData = { result: 'success', ['additional-info']: 'login success' };
let httpEvent:HttpResponse<any> = new HttpResponse<any>({body:responseData});
expect(helper.sendMessage).toHaveBeenCalled();//ERROR here
const mockReq:TestRequest = httpMock.expectOne(environment.apiUrl+environment.signinUserUrl); //Expect that a single request has been made which matches the given URL, and return its mock
//once mocking of sending request is done, mock receiving a response. This will trigger the logic inside subscribe function
mockReq.flush(httpEvent); //flush method provides dummy values as response
httpMock.verify();//verify checks that there are no outstanding requests;
});
});
看来正确的方法是spyOn(helper,'sendMessage');
,而不是spyOn(helper.sendMessage,'sendMessage');