Angularjs 使用解析依赖项测试控制器

Angularjs 使用解析依赖项测试控制器,angularjs,unit-testing,jasmine,angular-routing,Angularjs,Unit Testing,Jasmine,Angular Routing,我正在尝试使用Jasmine对一个依赖于解析键的控制器进行单元测试。我也在使用controllerAs语法。路由代码如下所示: $routeProvider.when('/questions', { templateUrl: 'questions/partial/main_question_viewer/main_question_viewer.html', controller:'MainQuestionViewerCtrl', controllerAs:'questi

我正在尝试使用Jasmine对一个依赖于解析键的控制器进行单元测试。我也在使用controllerAs语法。路由代码如下所示:

$routeProvider.when('/questions', {
    templateUrl: 'questions/partial/main_question_viewer/main_question_viewer.html',
    controller:'MainQuestionViewerCtrl',
    controllerAs:'questionCtrl',
    resolve: {
        default_page_size: ['QuestionService', function (QuestionService) {
            //TODO Work out page size for users screen
            return 50;
        }],
        starting_questions: ['QuestionService', function (QuestionService) {
            var questions = [];
            QuestionService.getQuestions(1).then(
                function(response){
                    questions = response;
                }
            );
            return questions;
        }],
    },
});
到目前为止,主计长:

angular.module('questions').controller('MainQuestionViewerCtrl',
[
    'QuestionService',
    'starting_questions',
    'default_page_size',


    function (QuestionService, starting_questions, default_page_size) {

        var self = this;

        //Model Definition/Instantiation
        self.questions = starting_questions;
        self.page_size = default_page_size;
        self.filters = [];

        //Pagination Getters (state stored by QuestionService)
        self.current_page = function(){
            return QuestionService.get_pagination_info().current_page_number;
        }
        self.page_size = function(page_size){
            if(page_size != null){
                QuestionService.set_page_size(page_size);
            }
            return QuestionService.get_page_size();
        }
    }
]
);
以及测试代码:

describe('MainQuestionViewerCtrl', function () {

//===============================TEST DATA=====================================
var allQuestionsResponsePage1 = {
    count: 4,
    next: "https://dentest.com/questions/?format=json&page=2&page_size=1",
    previous: null,
    results: [
        {
            id: 1,
            subtopic: {
                topic: "Math",
                name: "Algebra"
            },
            question: "if a=3 and b=4 what is a+b?",
            answer: "7",
            restricted: false
        }
    ]
};

beforeEach(module('questions'));
beforeEach(module('globalConstants')); //Need REST URL for mocking responses

var ctrl, qService;
var backend,baseURL;

//inject dependencies
beforeEach(inject(function ($controller, $httpBackend,REST_BASE_URL) {
    ctrl = $controller('MainQuestionViewerCtrl');
    backend = $httpBackend;
    baseURL = REST_BASE_URL;
}));

//inject QuestionService and set up spies
beforeEach(inject(function (QuestionService) {


    qService = QuestionService;
}));

//Convenience for adding query params to mocked requests
var buildParams = function (page, page_size) {
    var params = {
        format: 'json',
        page: page,
        page_size: page_size,
    };

    var keys = Object.keys(params).sort(); //how angular orders query params
    var returnString = '?' + keys[0] + '=' + params[keys[0]] +
        '&' + keys[1] + '=' + params[keys[1]] + '&' + keys[2] + '=' + params[keys[2]];
    return returnString;
};

describe('Instantiation',inject(function ($controller) {
    beforeEach(module($provide){
    beforeEach(inject(function ($controller) {
        //Make a mock call to the server to set up state for the QuestionService
        backend.expectGET(baseURL + '/questions/' + buildParams(1, 1)).respond(200, allQuestionsResponsePage1);
        qService.getQuestions(1);
        backend.flush();

        //Now mock the result of resolve on route
        ctrl = $controller('MainQuestionViewerCtrl', {
            default_page_size: 1,
            starting_questions: allQuestionsResponsePage1,
        });
    }));

    it('should start with the first page of all the questions pulled down', function () {
        expect(qService.questions).toEqual(allQuestionsResponsePage1);
    });

    it('should start on page 1', function () {
        expect(qService.current_page).toEqual(1);
    });

    it('should start with the page size set to the default passed in',function(){
        expect(qService.page_size).toEqual(1);
    })
}));
当尝试运行测试时,Angular抱怨它无法解决起始问题或默认页面大小,因为它们的提供者未知


值得指出的是,模拟QuestionService的HTTP请求的原因是它基于响应构建分页信息,然后控制器将访问这些信息以确定UI中的分页器大小/编号。

已解决。我在外部描述中实例化了控制器,但没有为resolve键依赖项传递模拟值。这就是导致错误的原因:用mock dependecies实例化控制器的方法可以很好地工作