【问题标题】:How to define tests with Jasmine for $resource (AngularJS)如何使用 Jasmine 为 $resource (AngularJS) 定义测试
【发布时间】:2015-12-09 12:22:06
【问题描述】:

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

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

我的目的是检查我是否得到响应数据?那可能吗?同样在 Angular Doc 中,对于 $resource 建议使用 $httpBackend。这就是为什么我有点困惑如何开始定义测试。

起初我的服务:

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); }
);

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

【问题讨论】:

    标签: javascript angularjs unit-testing jasmine angularjs-service


    【解决方案1】:

    @downvoter 更新:

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

    我原来的答案:

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

    此测试必须在后端部分进行,Angular 中的服务假定它收到了一个好的答案,这就是为什么有 $httpBackend 来将您的服务逻辑与 REST API 隔离开来。

    【讨论】:

    • 测试后端代码的例子???使用后端提供的工具。正如我所解释的,您不必在 Angular 应用程序方面进行测试。
    【解决方案2】:

    @Moncef 说的是真的。您不应该使用 Angular 测试您的 REST API。服务器的工作是测试来自 REST API 的响应是否正确。但是,如果您仍然想测试它。

    How do I test an AngularJS service with Jasmine?

    【讨论】:

      【解决方案3】:

      您应该测试的是您的回调,以获得来自服务器的正确响应。 为此,您可以使用$httpBackend 模拟服务器端。

      您可以使用: $httpBackend.expect $httpBackend.when

      主要区别在于,第一个使请求成为强制性请求,如果没有发生则将进行失败测试,​​并且还将期望所有请求按照它(expect 函数)在场景中被调用的顺序, while when 更宽容,只是在发送请求时提供响应,否则什么都不做。

      您可以在Angular official documentation 了解更多关于差异的信息。

      编辑,期望 - 响应工作流程的完整示例:

      //Group controller spec... success test...
      $httpBackend.expect('GET', '/groups').respond('200', {'groups': [{'name': 1}, {'name': 2}]});
      //above line tests that the groups controller asks for groups from the backend
      
      $httpBackend.flush();
      //above line makes the actual assertion that the request was sent, and sends the mocked response code and data
      
      expect($scope.groups).toEqual([{'name': 1}, {'name': 2}]);
      

      失败:

        //Group controller spec... failure callback test...
          $httpBackend.expect('GET', '/groups').respond('500', {'error': 'server error'});
          //above line tests that the groups controller asks for groups from the backend
      
          $httpBackend.flush();
          //above line makes the actual assertion that the request was sent, and sends the mocked response code and data
      
          expect($scope.groups).toBeUndefined();
          //can also spyOne the error handler, and assert that it was triggered...
      

      【讨论】:

      • 完成,请参阅编辑
      • 谢谢 Oleg.. 我会在我的 Angular 应用程序中尝试您的示例,并尽快通知您。
      • @OlegTikhonov 我引用了他的问题:“我的目的是检查我是否得到响应数据?这可能吗?”在这里,您正在嘲笑后端,而不是检查它是否回来了。所以,你没有回答他的问题。使用角度单元测试框架测试后端是否正确?
      • 我想你误解了这个问题,他显然是在问我们俗称的mocking,作者还附上了$httpBackend的引用。
      • @MoncefHassein-bey 我的目的是测试正确的回调。这意味着我想测试请求是否正确定义了服务。对不起,我是编写测试的新手。。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多