Mocha Chai test case for angular configuration file
Asked Answered
S

2

0

I am getting a hard time to resolve mocha chai test case for angular js configuration file .

angular.module('myApp.myModule', []).config(function ($stateProvider, $urlRouterProvider) {
$stateProvider.state('myModule', {
  url: '/myModule',
  templateUrl: function(){
    return 'scripts/my-module/views/my-module.html';
  },
  controller: 'myModuleCtrl',
  controllerAs: 'myCtrl',
  css: 'styles/lazy-load/my-module.css'
});
$urlRouterProvider.otherwise('/');

})

I need to cover mocha chai test case for return function of "templateUrl" which is returning a url ('scripts/my-module/views/my-module.html').

enter image description here

Sterculiaceous answered 19/2, 2015 at 12:10 Comment(2)
Are you getting some error. I am not sure what you are trying to test.Shading
I have changed templateUrl : "scripts/my-module/views/my-module.html" to templateUrl: function(){ return 'scripts/my-module/views/my-module.html';} which reduces Karma test code coverage. so i am trying to cover this function through my controller. without return type function to templateUrl code coverage was full.Sterculiaceous
S
0

I did it like

it('should load the page.', inject(function ($state) {
    var state = $state.get('selectLine');
    assert.isDefined(state.templateUrl()); 
    expect(state.templateUrl()).to.equal('scripts/select-line-module/views/select-line.html');
}));

This works for me

Sterculiaceous answered 26/2, 2015 at 3:43 Comment(0)
S
0

What you can do is to inject $location and $route service into your test. Setup a whenGET to intercept the actual request and then check the $location and $route configuration after the transition is complete. I did something similar earlier

it("should load the page.", inject(function ($rootScope, $location, $route, $httpBackend) {
        $httpBackend.whenGET("scripts/my-module/views/my-module.html").respond("<div/>");
        $location.path("/myModule");
        $rootScope.$digest();
        expect($location.path()).toBe("/myModule");
    }));

I was using $route service you would require $state service.

Shading answered 25/2, 2015 at 11:3 Comment(6)
Getting an Error : [$injector:unpr]Unknown provider: $routeProvider <- $routeSterculiaceous
As i said inject the $state service not $route.Shading
it("should load the page.", inject(function ($location, $rootScope, $state, $httpBackend) { $httpBackend.whenGET("scripts/my-module/views/my-module.html").respond("<div/>"); $location.path("/my-module"); $rootScope.$digest(); expect($location.path()).toBe("/my-module"); })); This gives undefined is not a function at expectSterculiaceous
This test was against jasmine, may the assertion statements in mocha are different.Shading
i am using mocha-chaiSterculiaceous
i created different thread ,that may explain better .... #28719608Sterculiaceous
S
0

I did it like

it('should load the page.', inject(function ($state) {
    var state = $state.get('selectLine');
    assert.isDefined(state.templateUrl()); 
    expect(state.templateUrl()).to.equal('scripts/select-line-module/views/select-line.html');
}));

This works for me

Sterculiaceous answered 26/2, 2015 at 3:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.