2016-08-20 161 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 /你能幫我嗎? –