AngularJS: Angular UI Router load multiple templates for testing -


i'm trying perform simple test on helper function i've created:

    function getpostid() {         return $stateparams._id;     } 

and here test using, please note getservice wrapper angular inject() function:

describe('wpapp router', function() {      var $state, $stateparams, router;      beforeeach(module('wpapp.core.router'));     beforeeach(function() {         $state = getservice('$state');         $scope = getservice('$rootscope');         $stateparams = getservice('$stateparams');         router = getservice('router');         $templatecache = getservice('$templatecache');          $templatecache.put('/app/sections/posts/list/post-list.html', '');     });      it('should provide current post id', function() {         $state.go('posts.details', { _id: 1 });         $scope.$digest();         expect(router.getpostid()).to.equal(1);     }); }); 

my router looks this:

    $stateprovider         .state('posts', {             url: '/posts',             controller: 'postslistcontroller postslist',             templateurl: '/app/sections/posts/list/post-list.html'         })          .state('posts.details', {             url: '/:_id',             controller: 'postdetailscontroller postdetails',             templateurl: '/app/sections/posts/details/post-details.html'         }) 

i'm trying set current state posts.details _id of 1. test helper function see if correctly retrieving id.

however, getting errors templates of states, following: unexpected request: /app/sections/posts/details/post-details.html

the state posts.details child of posts , both have own templates, problem unable load both templatecache or @ least can't figure out how so.

is possible load both templates in order fulfill test?

in response actual question, easiest approach caching templates using karma-ng-html2js-preprocessor. cache application templates, , make them available tests module.

however, can surmise code provided, un-necessary. currently, unit test integration test in it's dependent on routing of 'posts.details' state in order pass, , yet method concerned $stateparams object.

so, instead, $stateparams in fact plain object, simpler (and better) approach mock prior test:

beforeeach(module(function($provide) {   $provide.value('$stateparams', {     _id: 1   }); })); 

and can test service method in isolation states:

it('should provide current post id', function() {    expect(router.getpostid()).toequal(1); ); 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -