如何为角度ui路由器单元测试正确注入$ state?

时间:2015-02-26 11:26:25

标签: angularjs unit-testing angular-ui-router

我正在尝试为以下用例编写单元测试:

http请求以非协议的http状态响应(401) - >将状态更改为登录

问题是,我甚至没有在我的测试中注入正确的$ state参数。

describe('Login: ', function() {
beforeEach(module('myapp'));


function mockTemplate(templateRoute, tmpl) {
    $templateCache.put(templateRoute, tmpl || templateRoute);
}

describe('On HTTP 401', function(){
    var state;

    beforeEach(function(){
        mockTemplate.bind('components/splitapp/splitappView.html', '')
        mockTemplate.bind('components/splitappMaster/splitappMasterView.html', '')
        mockTemplate.bind('components/splitappDetail/splitappDetailView.html', '')
        mockTemplate.bind('components/splitapp/offcanvasMenu.html', '')
    })


    it('Should go to login-state', inject(function($state, $location){
        console.log($location.url())
        $state.go("splitapp")
        console.log($state.current)
        expect($state.is('splitapp')).toEqual(true);
    }))
})
});

在$ state.go之后,状态应该是splitapp,它在路由器配置中正确定义。继承人我的测试员输出

LOG: ''
LOG: Object{name: '', url: '^', views: null, abstract: true}
Chrome 40.0.2214 (Mac OS X 10.10.2) Login:  On HTTP 401 Should go to login- 
state FAILED
    Expected false to equal true.
    Error: Expected false to equal true.

感谢您的帮助。

0 个答案:

没有答案