如何使用另一个服务参数监视和锁定服务?例如,我的新Authservice具有以下参数:
export class AuthService{
constructor(public serviceAbcd: ServiceAbcd) {}这是一个示例资源,在AuthService上没有构造函数参数。
https://codecraft.tv/courses/angular/unit-testing/mocks-and-spies/
describe('Component: Login', () => {
let component: LoginComponent;
let service: AuthService;
let spy: any;
beforeEach(() => {
service = new AuthService();
component = new LoginComponent(service);
});
it('needsLogin returns true when the user has not been authenticated', () => {
spy = spyOn(service, 'isAuthenticated').and.returnValue(false);
expect(component.needsLogin()).toBeTruthy();
expect(service.isAuthenticated).toHaveBeenCalled();
});
afterEach(() => {
service = null;
component = null;
});
});我们正在尝试在没有测试床https://dev.to/angular/unit-testing-in-angular-to-testbed-or-not-to-testbed-3g3b的情况下进行测试
发布于 2021-06-04 11:14:46
你能做service = new AuthService()吗?它不会抱怨它需要一个参数?
尝试执行以下操作:
service = new AuthService({/* mock ServiceAbcd here */});https://stackoverflow.com/questions/67829672
复制相似问题