如何使用 Jasmine 为 $resource (AngularJS) 定义测试 [英] How to define tests with Jasmine for $resource (AngularJS)

查看:25
本文介绍了如何使用 Jasmine 为 $resource (AngularJS) 定义测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的 Angular 应用定义测试,但我不知道如何使用,因为这是我第一次编写测试.

I want to define tests for my angular app but I don't know how to work with because it's my first time to write tests.

但是,在我的应用程序中使用 Jasmine 为 REST 服务定义测试对我来说很重要.好吧,我的问题是如何测试 GET 请求.我在茉莉花网站上并阅读了那里的样本.但这只是测试 URL 是否正确写入.

However, it's important for me to define Tests with Jasmine for REST Service in my application. Well, my question is how can I test the GET requests. I was on the jasmine site and have read the sample there. But it was only a test whether the URL is correctly written.

我的目的是检查我是否得到响应数据?那可能吗?同样在 Angular Doc 中写到 $resource 是 $httpBackend 推荐.这就是为什么我对如何开始定义测试感到有些困惑.

My intention is to check if I get response data back? Is that possible? Also in Angular Doc is written that for $resource is $httpBackend recommended. That's why I'm a bit confused how starting define the tests.

起初我的服务:

resService:

testApp.factory('ResService', ['$resource', 'baseUrl',
    function ($resource, baseUrl) {
        return {
            group: $resource(baseUrl + '/api/qr_group/:Id', {
                Id: '@Id'
            }, {})
        }
    }]);

CrudService:

...
getAllGroups: function () {
    return ResService.group.query();
},

最后我的控制器提出了请求:TestCtrl:

At last my Controller with the request: TestCtrl:

CrudService.getAllGroups().$promise.then(
      function (response) { $scope.groups = response; },
      function (error) { errMessage(error); }
);

有人有想法吗?这会很有帮助:)

Do anyone has an idea? It would be helpful :)

推荐答案

@downvoter 的更新:

我引用了他的问题:我的目的是检查我是否得到了响应数据?这可能吗?"使用 Angular 单元测试框架测试后端是否正确?

I quote from his question : "My intention is to check if I get response data back? Is that possible?" Is testing the backend with angular unit test framework correct ?

我原来的回答:

您正在尝试测试您的 REST API,而不是前端部分(角度应用.).

You are trying to test your REST API and not the frontend part (angular app.).

这个测试必须在后端部分,Angular 中的服务假设它收到了很好的答案,这就是为什么有 $httpBackend,将您的服务逻辑与 REST API 隔离.

This tests must be on backend part, the service in Angular assumes that it receive a good answer, this is why there is $httpBackend, to isolate your service logic from the REST API.

这篇关于如何使用 Jasmine 为 $resource (AngularJS) 定义测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆