简体   繁体   English

在模块中模拟导出的功能

[英]Mock exported function in module

I have two modules which contain exported functions. 我有两个包含导出功能的模块。 "ModuleB" uses a function from "ModuleA". “ ModuleB”使用“ ModuleA”中的函数。 Now I want to test "ModuleB" and mock the used function from "ModuleA". 现在,我想测试“ ModuleB”并从“ ModuleA”模拟使用的函数。

I use ES6 with babel. 我将ES6与babel一起使用。 For testing I use karma and jasmine. 为了测试,我使用业力和茉莉花。

I tried using babel-rewire and inject-loader, but it just does not work. 我尝试使用babel-rewire和inject-loader,但是它不起作用。 I'm kind of new to all this and I guess I'm just doing something wrong. 我对这一切还是陌生的,我想我做错了什么。 So any help is appreciated! 因此,任何帮助表示赞赏!

moduleA.js moduleA.js

export function getResult() {
    return realResult;
}

moduleB.js moduleB.js

import * as ModuleA from './moduleA'

export function functionToTest() {
    let result = ModuleA.getResult();
    // do stuff with result which I want to test
}

my test 我的测试

it("does the right thing", () => {
    // I tried using rewire, but this gives me an exception:
    ModuleB.__Rewire__('ModuleA', {

        getResult: () => {
            return fakeResult;
        }
    });

    let xyz = ModuleB.functionToTest(canvas, element);
});

Take a look to this library https://www.npmjs.com/package/mockery I thought It's exactly what do you want. 看看这个库https://www.npmjs.com/package/mockery我想这正是您想要的。 Hope it helps! 希望能帮助到你!

EDITED: 编辑:

Basically you have to use mockery in each test to mock your function, the only problem is that you must use require against import with the modules you want to mock. 基本上,您必须在每个测试中都使用嘲讽来模拟您的函数,唯一的问题是您必须对要模拟的模块使用“要求禁止导入”。 Look this example: 看这个例子:

const moduleUnderTest = './moduleB.js';
const moduleA_fake = { getResult: () => { return "This is a fake result"; } } ;

describe("Mocking functions", () => {

    it('Should be Fake Result', (done) => {

        mock.registerAllowable(moduleUnderTest);
        mock.registerMock('./moduleA.js', moduleA_fake);

        mock.enable({ useCleanCache: true });

        let ModuleB = require(moduleUnderTest);
        let res = ModuleB.functionToTest();

        res.should.be.eql("This is a fake result");


        mock.disable();
        mock.deregisterAll();

        done();
    });

    it('Should be Real Result', (done) => {

        let ModuleB = require(moduleUnderTest);
        let res = ModuleB.functionToTest();

        res.should.be.eql("real foo");

        done();
    });

});

You could see the complete example Here 您可以在此处看到完整的示例

For anyone who is interested in how it works with babel-rewire : The trick is to use __RewireAPI__ . 对于对babel-rewire __RewireAPI__工作方式感兴趣的人:诀窍是使用__RewireAPI__

import * as ModuleB from '../ModuleB'
import {__RewireAPI__ as ModuleBRewire} from '../ModuleA';

it("does the right thing", () => {
    let moduleAMock = {
        getResult: () => {
            return fakeResult;
        }
    }

    ModuleBRewire.__Rewire__('ModuleA', moduleAMock);

    let xyz = ModuleB.functionToTest();
});

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

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