简体   繁体   English

Mocha Chai角度配置文件的测试用例

[英]Mocha Chai test case for angular configuration file

I am getting a hard time to resolve mocha chai test case for angular js configuration file . 我很难解决角度js配置文件的mocha chai测试用例。

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'). 我需要为“templateUrl”的返回功能覆盖mocha chai测试用例,该模板返回一个url('scripts / my-module / views / my-module.html')。

在此输入图像描述

What you can do is to inject $location and $route service into your test. 你可以做的是在你的测试中注入$location$route服务。 Setup a whenGET to intercept the actual request and then check the $location and $route configuration after the transition is complete. 设置whenGET来拦截实际请求,然后在转换完成后检查$location$route配置。 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. 我使用$route服务你需要$state服务。

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 这适合我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM