本文介绍了如何使用ngRedux测试服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用业力茉莉花来模拟ngRedux的getState

How can i mock getState of ngRedux: with karma jasmine

例如,我的服务通过ngRedux从商店中获得价值.

For example my service get value from store by ngRedux.

this.ngRedux.getState()

推荐答案

我通过监视getState()方法解决了这个问题:

I solved the problem by spying on the getState() method:

spyOn(MockNgRedux.getInstance(), 'getState').and.returnValue({ ... });

我不知道这是否是这样做的方式,但是对于我来说,它是可行的.不过,我不需要模拟任何奇特的状态修改,只需一个简单的getState().

I have no idea if that's the way to do it, but it worked in my case. I don't need to mock any fancy state modifications, though, just one simple getState().

MockNgRedux@angular-redux/store模块的一部分.您的测试可能看起来像这样:

MockNgRedux is part of the @angular-redux/store module. Your test could look somewhat like this:

...
import {MockNgRedux, NgReduxTestingModule} from '@angular-redux/store/lib/testing';

describe('MyTest', () => {
    beforeEach(async(() => {
        TestBed.configureTestingModule({
            ...
            imports: [
                ...
                NgReduxTestingModule
            ],
            providers: [
                ...
                MockNgRedux
            ]
        })
        .compileComponents();
    }));

    it('does something', () => {
        // set up
        spyOn(MockNgRedux.getInstance(), 'getState').and.returnValue({here: 'be my mock store'});
        spyOn(MockNgRedux.getInstance(), 'dispatch').and.stub();
        ....
        expect(MockNgRedux.getInstance().dispatch).toHaveBeenCalledWith(something);
    });
});

以下是文档的链接: https://angular-redux. github.io/store/classes/mockngredux.html

这篇关于如何使用ngRedux测试服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 11:46