加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – Karma jasmine angular ui router – 使用params测

发布时间:2020-12-17 17:53:36 所属栏目:安全 来源:网络整理
导读:我的角度模块“moduleB”中的状态定义如下 $stateProvider .state('stateB',{ parent: 'stateA',abstract: true,templateUrl : baseUrl+'/templates/stateB.html' }) .state('stateB.details',{ url: '/stateB/details/:param1/:param2',resolve : { value3
我的角度模块“moduleB”中的状态定义如下

$stateProvider
                .state('stateB',{
                    parent: 'stateA',abstract: true,templateUrl : baseUrl+'/templates/stateB.html'
                })
                .state('stateB.details',{
                    url: '/stateB/details/:param1/:param2',resolve : {
                        value3 : ['$localStorage','$stateParams',function($localStorage,$stateParams){
                            return $localStorage.value3[$stateParams.param1];
                        }]
                    },views : {
                        'view1' : {
                            templateUrl : baseUrl+'/templates/view1.html',controller: 'View1Ctrl'
                        },'view2' : {
                            templateUrl : baseUrl+'/templates/view2.html',controller : 'View2Ctrl'
                        }
                    }
                })

我想为“解决”编写单元测试,这是我的茉莉花单元测试.

var rootScope,state,injector,mockLocalStorage,httpBackend;
beforeEach(module('moduleB'));
 beforeEach(inject(function($rootScope,$state,$injector,$localStorage,$httpBackend) {
            rootScope = $rootScope;
            state = $state;
            injector = $injector;
            httpBackend = $httpBackend;
            mockLocalStorage = $localStorage;
        }));

it('should should resolve the data',function() {
            mockLocalStorage.value3 = {};
            mockLocalStorage.value3["1234567890"] = 'resolved-data';
            state.go('stateB.details',{
                                            "param1" : "1234567890","param2" : true
                                      });
            rootScope.$digest();


            console.log('state',state);
            expect(state.current.name).toBe('stateB.details');
             expect(injector.invoke(state.current.resolve.value3)).toBe('resolved-data');

        });

1)console.log(‘state’,state)==>版画
‘state’,Object {params:Object {},current:Object {name:”,……..}

2)expect(state.current.name).toBe(‘stateB.details’)==>失败,错误
???期待”成为’stateB.details’.

3)期待解决==>失败,错误
???TypeError:’undefined’不是对象(评估’state.current.resolve.value3′)

任何人都可以帮助指出我错过了什么?

更新:

我修改了我的测试,在promise的成功块中有断言.
即使断言是假的,它也会通过测试.

it('should resolve the data',"param2" : true
                                      }).then(function() {
                                                    console.log('state',state);
             expect(state.current.name).toBe('stateB.details');                                         expect(injector.invoke(state.current.resolve.value3)).toBe('resolved-data2'); 
                                             });;
        });

此测试通过,并且没有显示“console.log(‘state’,state);” .
而且,我对“已解决的数据2”的断言应该失败,因为期望的值是“已解析数据”.

解决方法

最后,我解决了这个问题.这是任何感兴趣的人的解决方案.

1)导入模块

beforeEach(module('stateA'));
   beforeEach(module('stateB'));
   beforeEach(module('ui.router'));

2)

beforeEach(module(


function($provide) {
            $provide.value('$localStorage',mockStorage = { value3: []});
            $provide.value('$stateParams',stateParams = { param1: 1234,param2: "true"});
        }));

3)单元测试:

it('should go to stateB.details state and resolve the data',function() {
            mockStorage.assetInfo[stateParams.value3] = 'resolved-data';
            var s = state.get('stateB.details');
            expect(injector.invoke(s.resolve.value3)).toEqual('resolved-data');
        });

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读