javascript - 将 $stateParams 和 $state 注入(inject) Jasmine Angular js 测试变得未定义

标签 javascript angularjs unit-testing jasmine

我正在为我的 DetailCtrl 编写 jasmine 测试。我有 10 个 json 文件,每个文件的文件名都是这样

1.json
2.json
3.json

在我的数据文件夹中

这是我的详细控制

backpagecontrollers.controller('DetailCtrl', function($scope, $stateParams, $http) {
  $http.get('data/' +  $stateParams.listingId + '.json').success(function(data) {
      $scope.extrainfo = data; 
  });
}); 

细节 Controller 正在从我的数据文件夹中获取每个 1.json、2.json、3.json 文件。

这是我的部分路线

.state('listingdetail', {
      url: "/listings/:listingId",
      templateUrl: "partials/detail.html",
      controller: 'DetailCtrl'
    })

让我们回到测试中,我将 $stateParams$state 都注入(inject)了​​测试。

我想测试上面的每个 json 文件是否存在我的 json 文件中的图像。 我正在设置 httpbackend 以获取本地主机 url 加上来自 $stateparams 的 listingId,我将其配置为路由的一部分,但 listingId 返回时未定义。我应该在我的测试中注入(inject)其他东西吗?

describe('Detail Ctrl', function() {

      var scope, ctrl, httpBackend, stateparams, listingId; 

      beforeEach(angular.mock.module("backpageApp"));
      beforeEach(angular.mock.inject(function($controller, $rootScope, _$httpBackend_,    $stateParams, $state) {
        httpBackend = _$httpBackend_;
        stateparams = $stateParams; 
        listingId = stateparams.listingId;

        httpBackend.expectGET('http://localhost:8000/#/listings/' + listingId).respond([{id: 1 }, {id: 2}, {id:3}, {id:4}, {id:5}, {id:6}, {id:7}, {id:8}, {id:9}, {id:10}]);
        scope = $rootScope.$new(); 
        ctrl = $controller("DetailCtrl", {$scope:scope}); 
      }));

       it('the images for each listing should exist', function() {
        httpBackend.flush(); 
        expect(scope.images).toBe(true)
      });
    });

我收到这个错误

Error: Unexpected request: GET data/undefined.json
    Expected GET http://localhost:8000/#/listings/undefined

最佳答案

我认为您可能误解了路由器是如何与 Controller 协同工作的。当您对 Controller 进行单元测试时,您不会执行路由或进入 ui-router 状态。这些状态和路由是应用程序正常运行时触发 Controller 执行的内容。但是在单元测试中,您正在使用 $controller 显式执行 Controller 。所以你完全跳过了路由部分。这意味着您需要模拟 ui-router 通常会为您创建的对象 $stateparams。

describe('Detail Ctrl', function() {

  var scope, ctrl, httpBackend, stateparams, listingId; 

  beforeEach(angular.mock.module("backpageApp"));
  //don't need to inject state or stateparams here
  beforeEach(angular.mock.inject(function($controller, $rootScope, _$httpBackend_) {
    httpBackend = _$httpBackend_;
    stateparams = { listingId: 1 }; //mock your stateparams object with your id

    //you should be expecting the get request url from the controller, not the route
    httpBackend.expectGET('data/' + stateparams.listingId + '.json').respond([{id: 1 }, {id: 2}, {id:3}, {id:4}, {id:5}, {id:6}, {id:7}, {id:8}, {id:9}, {id:10}]);
    scope = $rootScope.$new(); 
    //pass your mock stateparams object to the controller 
    ctrl = $controller("DetailCtrl", {$scope:scope, $stateParams:stateparams}); 
  }));

   it('the images for each listing should exist', function() {
    httpBackend.flush(); 
    //I don't see images set in your controller, but you 
    //could check scope.extrainfo here
    expect(scope.images).toBe(true)
  });
});

关于javascript - 将 $stateParams 和 $state 注入(inject) Jasmine Angular js 测试变得未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21317377/

相关文章:

javascript - AngularJS单元测试: Initialize scope of a directive's controller

javascript - 使用 Angularjs 在嵌套数组中动态添加子项

asp.net-mvc - 在 ASP.NET MVC 2 中测试模型绑定(bind)

javascript - jquery get 请求返回文档而不是 XML

javascript - 通过属性名称数组访问嵌套对象

python - 使用 angularjs 和 flask 进行用户身份验证的正确方法

java - JUnit 如何测试迭代器

python - 单元测试 Flask 应用程序时的模板

javascript - 单击时所有元素都会更改,而不仅仅是目标元素

javascript - 如何创建与其他 View Controller 通用的基本 Ext.app.ViewController?