注入$ stateParams和$ state到jasmine angular js test得到undefined

时间:2014-01-23 19:16:36

标签: javascript angularjs unit-testing jasmine

我正在为我的DetailCtrl编写一个茉莉花测试。我有10个json文件,每个文件都有这样的文件名

1.json
2.json
3.json

在我的数据文件夹中

这是我的细节Ctrl

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

详细控制器从我的数据文件夹中获取每个1.json,2.json,3.json文件。

这是我路线的一部分

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

让我们回到测试,我将$stateParams$state注入测试。

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

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

2 个答案:

答案 0 :(得分:30)

我认为您可能误解了路由器如何使用控制器。当您对控制器进行单元测试时,您没有执行路由或进入ui-router状态。这些状态和路由是应用程序正常运行时触发控制器的内容。但是在单元测试中,您使用$ 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)
  });
});

答案 1 :(得分:1)

添加stateMock.js然后包含模块

beforeEach(function() {
    module('stateMock');
    module('mean');
    module('mean.system');
    module('mean.companies');
  });

这里的代码为stackMock.js:github code for stateMock

参考:UI-router interfers with $httpbackend unit test, angular js