【发布时间】:2018-01-10 02:48:54
【问题描述】:
所以,在测试中我已经为 AuthService 提供了一个模拟类
{ provide: AuthService, useClass: AuthServiceMock }
这个模拟服务有一个 isAuthorized() 函数,它总是return true;
而且,在规范中,看起来像这样
it('init root with LOGIN PAGE if is authenticated, () => {
expect(comp['rootPage']).toBe(LoginPage); // true!
});
it('init root with WELCOME PAGE if is not authenticated, () => {
// Here I need to change the result of isAuthorized()
// so inside the AuthServiceMock returns false
expect(comp['rootPage']).toBe(WelcomePage); // false :(
});
编辑:添加了描述的完整代码
describe('Component: Root Component', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyApp],
providers: [
{ provide: AuthServiceProvider, useClass: AuthServiceProviderMock },
ConfigProvider,
StatusBar,
SplashScreen
],
imports: [
IonicModule.forRoot(MyApp)
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyApp);
comp = fixture.componentInstance;
});
it('initialises with a root page of LoginPage if not authorized', () => {
expect(comp['rootPage']).toBe(LoginPage);
});
});
【问题讨论】:
标签: angular unit-testing service mocking