如何使用 Jasmine 为 $resource (AngularJS) 定义测试

编程入门 行业动态 更新时间:2024-10-28 18:31:56
本文介绍了如何使用 Jasmine 为 $resource (AngularJS) 定义测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想为我的 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-21 14:49:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1004772.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   定义   测试   resource   Jasmine

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!