如何定义测试,茉莉花为$资源(​​AngularJS) [英] How to define tests with Jasmine for $resource (AngularJS)

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

问题描述

我想定义测试我的角度应用程序,但我不知道如何使用,因为这是我第一次写的测试工作。

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.

然而,这非常重要,我在我的应用程序中定义茉莉试验REST服务。好吧,我的问题是如何测试GET请求。我是在茉莉现场,看了样品那里。但它只是一个测试该网址是否正确写入。

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.

我的目的是要检查,如果我得到响应的数据?那可能吗?此外,在角文件被写为$资源为$ 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

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

做任何人有一个想法?这将是有益的:)

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

推荐答案

更新为@downvoter:

我从他的问题引述:我的目的是检查,如果我得到响应数据回这可能吗?正在测试与角单元测试框架后端是否正确?

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.).

此测试必须在后端部分,在角服务假定它得到一个很好的答案,这就是为什么$ 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.

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

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