【问题标题】:Karma + jasmine + angular + ui router - testing for resolve with state.go with paramsKarma + jasmine + angular + ui 路由器 - 使用 state.go 和参数测试解析
【发布时间】:2015-06-15 05:20:16
【问题描述】:

我在我的角度模块“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'
                        }
                    }
                })

我想为“resolve”编写单元测试,这是我的 jasmine 单元测试。

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', function() {
            mockLocalStorage.value3 = {};
            mockLocalStorage.value3["1234567890"] = 'resolved-data';
            state.go('stateB.details', {
                                            "param1" : "1234567890",
                                            "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);”没有显示任何内容. 而且,我对“resolved-data2”的断言应该失败,因为预期值为“resolved-data”。

【问题讨论】:

  • 谁能帮我解决这个问题?
  • 不如state.go('stateB.details', { ... }).then(function() { ... }),或者使用状态改变事件?在这种情况下,我不认为对 $digest 的一次调用会减少它。
  • 你能举个详细一点的例子吗?使用您的方法,我应该在哪里检查承诺的成功块中的断言?我应该在哪里调用 $digest?
  • 尝试将断言立即放入 promise 的回调中。至此状态应完全解决。其他一切保持原样。
  • 我试过这个并用结果更新了我的问题。即使断言为假,它也会通过测试。我错过了什么吗?请查看更新。

标签: angularjs unit-testing karma-jasmine


【解决方案1】:

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

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');
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 2018-12-05
    • 2017-02-24
    • 2018-01-31
    • 2017-06-10
    相关资源
    最近更新 更多