【发布时间】:2019-09-18 23:42:36
【问题描述】:
我有一个对我的后端进行 HTTP 调用的服务,我正在尝试测试它是否会得到用户的响应,在运行测试时我得到一个 Spec has no expectation,即使我在订阅中有一个。所有这些测试都通过了,但 2 的输出为 SPEC HAS NO EXPECTATION
这是我的代码:
describe('Auth Service Testing', () => {
let httpClientSpy: { get: jasmine.Spy };
let authServ: AuthService;
let authAct: AuthActions;
let userAct: UserActions;
let checkoutAct: CheckoutActions;
let productAct: ProductActions;
let store: Store<any>;
let localStorageServ: LocalStorageService;
let authResponse;
const expectedUserResponse = {
users: [],
count: 25,
current_page: 1,
pages: 2
};
beforeEach(() => {
httpClientSpy = jasmine.createSpyObj('HttpClient', ['get']);
authServ = new AuthService(
<any>httpClientSpy,
authAct,
userAct,
checkoutAct,
productAct,
store,
localStorageServ
);
});
it('should get users response', () => {
httpClientSpy.get.and.returnValue(asyncData(expectedUserResponse));
authServ.authorized().subscribe((users) => {
authResponse = users;
expect(users).toEqual(jasmine.objectContaining({ users: [] }));
});
});
it('should equal to expected users response', () => {
expect(authResponse).toEqual(expectedUserResponse);
});
it('should return null if theres an error', () => {
httpClientSpy.get.and.returnValue(asyncError(expectedUserResponse));
authServ
.authorized()
.subscribe(() => {}, (error) => expect(error).toBe(null));
});
});
另外,我遵循了 Angular HTTP 测试指南 angular test 我想知道这是一个错误还是其他什么。
因果报应:
Auth Service Testing
SPEC HAS NO EXPECTATIONS should return null if there's an error
SPEC HAS NO EXPECTATIONS should get users response
should equal to expected users response
更新
缺少的代码是这个expect(httpClientSpy.get.calls.count()).toBe(1);这很奇怪,我以为这个调用发出了一个http get请求httpClientSpy.get.and.returnValue(asyncError(expectedUserResponse));
但是在指南的错误测试中,他们没有这个。有人能解释一下吗?
来自朝鲜的很多爱。
【问题讨论】:
标签: angular karma-jasmine angular-test