Mocha Chai test case for angular configuration file

好久不见. 提交于 2019-12-02 19:51:12

问题


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').


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/28606056/mocha-chai-test-case-for-angular-configuration-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!