【发布时间】:2017-06-20 10:03:40
【问题描述】:
我正在尝试测试我的组件,该组件的构造函数已注入名为 AzureService 的服务
这里是组件 sn-p:
constructor(private azureService: AzureService) { }
这是规范文件:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
DashbaordComponent,
StatusComponent
],
providers:[ AzureService]
}).compileComponents();
}));
it('should create the app', async(() => {
const fixture = TestBed.createComponent(DashbaordComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
这里是服务:
export declare var WindowsAzure;
@Injectable()
export class AzureService {
constructor() { this.client = new WindowsAzure.MobileServiceClient(this.azureUrl); }
}
我无法理解为什么在 index.html 文件中导入的 Azure-Apps-Client 库并不重要:
<script src="//zumo.blob.core.windows.net/sdk/azure-mobile-apps-client.2.0.0.js"></script>
有没有办法在我运行测试之前加载这个库?
这有什么线索吗?
更新:这是组件代码:
getModules() {
this.result.updateInfo("Getting classes...")
this.azureService.getProgrammesByWrapper().then(((res) => {
this.result.updateInfo("Sorting classes...")
this.displayModules(res);
this.result.updateSuccess(true);
}));
}
【问题讨论】:
-
你能发布你的组件代码吗?如果我可以看到您的组件如何与您的 AzureService 交互,我可以更具体地了解如何模拟该服务。
-
当然。我已经用我正在使用我的组件服务的组件的 sn-p 更新了我的问题。
-
太棒了。我已经更新了我的答案,以包含更多关于如何模拟服务以进行测试的具体示例。
标签: angular azure testing karma-jasmine