【问题标题】:Testing $resource without mock在没有模拟的情况下测试 $resource
【发布时间】: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


    【解决方案1】:

    done 用于异步测试。如果您的测试不是异步的,请不要使用done 例如:

        // No done needed
        it("should...", () => {
            expect(one).toBe("aValue");
        });
    

    对于异步测试,在异步操作完成后调用它。所以:

        beforeEach((done) => { //done is a parameter in beforeEach
            SelfRegistrationResourceBuilder.getCountries().get(
                (value: any) => {
                    console.log("no error");
                    one = "aValue";
                    done();  //  Yes call done
                }, (error: any) => {
                    console.log("error");
                    one = "aValue";
                    // do not call done. You most likely want to throw to show this is an error case. 
                })
        });
    

    【讨论】:

    • 感谢您的帮助,但这样做仍然给我同样的错误,我的“一个”变量仍然未定义
    • 但这一切都发生了,因为我的回调永远不会被调用
    • 阅读 this 告诉我这可能是不可能的
    猜你喜欢
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    相关资源
    最近更新 更多