2016-08-20 147 views
1

这是我的控制器在控制器测试模拟工厂

angular 
.module('studentsApp') 
.controller('StudentsController', StudentsController); 

function StudentsController($scope, StudentsFactory) { 
    $scope.students = []; 
    $scope.specificStudent= {}; 

    var getStudents = function() { 
     StudentsFactory.getStudents().then(function(response) { 
      if($scope.students.length > 0){ 
       $scope.students = []; 
      } 
      $scope.students.push(response.data); 
     }); 
    }; 
} 

这是我厂

angular.module('studentsApp') 
.factory('StudentsFactory', function($http) { 
    var base_url = 'http://localhost:3000'; 
    var studentsURI = '/students'; 
    var studentURI = '/student'; 
    var config = { 
    headers: { 
     'Content-Type': 'application/json' 
    } 
    }; 

    return { 
    getStudents: function() { 
     return $http.get(base_url + studentsURI); 
    } 
    }; 
}); 

这里是我如何试图单元测试控制器

describe('Controller: Students', function() { 
    var StudentsController, scope, StudentsFactory; 
    beforeEach(function() { 
    module('studentsApp'); 
    inject(function($rootScope, $controller, $httpBackend, $injector) { 
     scope = $rootScope.$new(); 
     httpBackend = $injector.get('$httpBackend'); 
     StudentsFactory = $injector.get('StudentsFactory'); 

     StudentsController = $controller('StudentsController', { 
     $scope : scope, 
     'StudentsFactory' : StudentsFactory 
     }); 

     students = [{ 
     name: 'Pedro', 
     age: 10 
     }, { 
     name: 'João', 
     age: 11 
     }, { 
     name: 'Thiago', 
     age: 9 
     }]; 

     spyOn(StudentsFactory, 'getStudents').and.returnValue(students); 
    }); 
    }); 

    it('Should get all students', function() { 
    scope.students = []; 

    StudentsController.getStudents(); 
    $scope.$apply(); 
    expect(scope.students.length).toBe(3); 
    }); 
}); 

的问题是当我运行测试,则显示以下消息:

未定义不是(评价 'StudentsController.getStudents()')

我看着一个构造整个互联网试图找到一个可以帮助我的教程,但我没有找到任何东西,有人可以帮我吗?

回答

0

它与getStudent()函数为private(由var声明)相关的事实的链接。因此您的测试无法访问它。您必须将其附加到$范围或这可以测试它。 我一般用这个控制器中:

var $this = this; 
$this.getStudents = function() { 
    ... 
}; 
+0

感谢@Gerald,我为你所提到的问题作出的回答,我'在测试中遇到以下消息时出现问题:** undefined不是构造函数(评估'StudentsFactory.getStudents()。then')**我该如何处理? –

+0

我会这样做:http://www.bradoncode.com/blog/2015/07/13/unit-test-promises-angualrjs-q/ –

0

有没有StudentsController.getStudents方法。它应该是

this.getStudents = function() { ... }; 

Mocked StudentsFactory.getStudents返回一个普通对象,但它应该返回一个承诺。

$controller不应该提供真实StudentsFactory服务为本地依赖性(它在默认情况下已经提供了它):现在

var mockedStudentsFactory = { 
    getStudents: jasmine.createSpy().and.returnValue($q.resolve(students)) 
    }; 

    StudentsController = $controller('StudentsController', { 
    $scope : scope, 
    StudentsFactory : mockedStudentsFactory 
    }); 
+0

我试过了,但是,它并没有增加学生的价值scope.students ..另一件事是。我没有使用$ q,我使用$ http,这是一个问题? –

+0

$ http返回$ q承诺,响应可能会受到$ q承诺的影响。 '学生'是你的案例中的一个数组,它有望成为一个拥有'data'属性的对象。此时它应该工作或抛出一个有意义的错误。如果这仍然不适合你,请发布一个小提琴/ plunk,可以显示你的情况出了什么问题。 – estus

+0

感谢,但还没有工作,它看起来是在调用承诺,但没有把数组的值放在scope.students中,我创建了一个小提琴,以便你可以更好地看到代码:https:// jsfiddle .net/z9k3c35c /你能帮我吗? –