angularjs - Testing controller with resolve dependencies -
i'm trying unit test controller relies on resolve keys using jasmine. using controlleras syntax. routing code follows:
$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 users screen return 50; }], starting_questions: ['questionservice', function (questionservice) { var questions = []; questionservice.getquestions(1).then( function(response){ questions = response; } ); return questions; }], }, });
the controller (so far):
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 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(); } } ] );
and test code:
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 , b=4 a+b?", answer: "7", restricted: false } ] }; beforeeach(module('questions')); beforeeach(module('globalconstants')); //need rest url 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 , set spies beforeeach(inject(function (questionservice) { qservice = questionservice; })); //convenience adding query params 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 mock call server set state questionservice backend.expectget(baseurl + '/questions/' + buildparams(1, 1)).respond(200, allquestionsresponsepage1); qservice.getquestions(1); backend.flush(); //now mock result of resolve on route ctrl = $controller('mainquestionviewerctrl', { default_page_size: 1, starting_questions: allquestionsresponsepage1, }); })); it('should start first page of 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 page size set default passed in',function(){ expect(qservice.page_size).toequal(1); }) }));
when trying run tests, angular complaining cant resolve starting_questions or default_page_size because providers them aren't known.
it worth pointing out reason mocking http request questionservice builds pagination info based on response, controller access determine paginator size/numbers in ui.
solved. instantiating controller in outer describe without passing in mock values resolve key dependecies. causing error: method of instantiating controller mock dependecies works fine.
Comments
Post a Comment