【问题标题】:Sinon spy: Attempted to wrap undefined property 'path' as functionSinon spy:试图将未定义的属性“路径”包装为函数
【发布时间】:2017-03-17 18:28:01
【问题描述】:

location.path 上的以下 spy 因错误而失败:

试图将未定义的属性路径包装为函数

it('should redirect location.path', function() {
 sinon.spy(location, 'path');
 scope.create();
 expect(location.path).to.have.been.calledOnce;
 location.path.restore();

模拟控制器:

describe('AdminUsersController', function() {
  var Users;
  beforeEach(inject(function(_$controller_, $rootScope, $location, _Users_, _$httpBackend_) {
    var location;
    scope = $rootScope.$new();
    location = $location;
    controller = _$controller_('AdminUsersController', {
      $scope: scope,
      $location: location
    });

我正在尝试测试的线路:

$scope.create = function() {
  return $location.path("admin/users/newuser?create");
};

【问题讨论】:

    标签: angularjs unit-testing sinon sinon-chai


    【解决方案1】:

    这个问题的解决方案是另一个愚蠢的咖啡脚本。我之前没有定义位置。我在coffeescript中的代码如下:

    describe 'AdminControllers', () ->
    
      scope = undefined
      location = undefined
      $httpBackend = undefined
    
      beforeEach module 'heliotrope.controllers.admin'
    
      describe 'AdminUsersController', () ->
    
        beforeEach inject (_$controller_, $rootScope, $location, _Users_, _$httpBackend_) ->
          scope = $rootScope.$new()
          location = $location
          controller = _$controller_ 'AdminUsersController', {
            $scope: scope
            $location: location
          }
          $httpBackend = _$httpBackend_
    
        describe 'creating a new user', () ->
    
          it 'should redirect location.path', () ->
            sinon.spy(location, 'path')
            scope.create()
            expect(location.path).to.have.been.calledOnce
            location.path.restore()
    

    【讨论】:

      猜你喜欢
      • 2020-07-19
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 2017-02-12
      相关资源
      最近更新 更多