【发布时间】:2016-03-30 21:15:54
【问题描述】:
我正在尝试通过 Angular 测试异步 $resource 服务。首先请注意,我不想模拟 $resource,我实际上是想获取结果并对其进行测试。
所以基本上我的 getCountries 方法只是使用 Angular 的 $resource 来返回一个国家数组。
方法
getCountries(): any {
var countries = this.$resource(
this.apiEndpoint.baseUrl,
{
'get': {
method: 'GET', isArray: false
}
});
return countries;
}
在我的规范中,我无法确定在哪里运行 Jasmine 的 done() 方法。请阅读代码中的cmets。
规格
describe(module', (): void => {
var one;
var SelfRegistrationResourceBuilder;
beforeEach((): void => {
angular.mock.module(
'BuilderModule',
'ngResource'
);
inject(
['Builder', (_SelfRegistrationResourceBuilder _: ISelfRegistrationResourceBuilder ): void => {
SelfRegistrationResourceBuilder = _SelfRegistrationResourceBuilder_;
}]);
});
describe('getCountries', (): void => {
beforeEach((done) => { //done is a parameter in beforeEach
SelfRegistrationResourceBuilder.getCountries().get(
(value: any) => {
console.log("no error");
one = "aValue";
done(); //Do I have to call done() here
}, (error: any) => {
console.log("error");
one = "aValue";
//done(); What about here
})
//done(); ..or here
});
it("should...", (done) => {
expect(one).toBe("aValue");
//done(); and here?
});
});
});
所以我的问题似乎是,当我运行我的代码时,我要么得到这个问题标题中所述的错误,要么如果我使用我调用的 done() 方法,那么我的名为“one”的变量是未定义的,因此暗示从未调用成功和错误回调。我还可以证明成功和错误回调永远不会被调用,因为它们的 console.log()s 不会打印到控制台。
最后,当我在浏览器中检查开发人员工具的网络部分时,我没有看到对服务器的任何请求。 有人可以帮我解决一下吗,我想我有除了调用 done() 之外,大部分都可以解决。
谢谢
【问题讨论】:
标签: angularjs asynchronous typescript jasmine angular-resource