模拟模块不存在? [英] mock module which does not exist?

查看:110
本文介绍了模拟模块不存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过以下方法在流星应用中运行摩卡测试时:

When i run my mocha tests in my meteor app by:

node_modules/.bin/mocha --compilers js:babel-core/register //..opts

我的被测模块要导入时出现问题:

i get a problem when my module under test wants to import:

import { Meteor } from 'meteor/meteor';

所以我试图用嘲讽来嘲笑它:

So i tried to mock it with mockery:

mockery.enable();
moduleUnderTest = '../moduleUnderTest';
mockery.registerAllowable(moduleUnderTest);
meteorMock = {};
mockery.registerMock('Meteor', meteorMock);

不幸的是找不到该模块

Error: Cannot find module 'meteor/meteor'

所以无法完成对流星的模拟.

So the mocking of Meteor cannot be done.

有没有一种方法可以伪造位置meteor/meteor?
(替代解决方案:如果我可以在摩卡测试中访问流星环境)

Is there a way how i can fake the location meteor/meteor?
(Alternate Solution: If i can get access to the Meteor Environment in my mocha test)

推荐答案

如果您查看

If you look at the documentation, you'll see that .registerAllowable takes a string, not a module. You also need to give the exact module name that you are mocking, and provide a fake module with the values you want.

所以:

var mockery = require("mockery");

mockery.enable();
mockery.registerAllowable("./moduleUnderTest");
// We want Meteor to have the value "foo". You probably want something
// different.
var meteorMock = { Meteor: "foo" };
// We mock 'meteor/meteor' because that's what's imported.
mockery.registerMock('meteor/meteor', meteorMock);

如果您考虑一下,您正在做的事情就行不通了.您需要将模块之前配置为模拟'Meteor'的Mockery,因此Node会加载模块,然后尝试在模拟可用之前加载Meteor,这样会失败.

If you think about it, what you were doing cannot work. You were requiring the module before Mockery is configured for mocking 'Meteor', so Node loads your module, and then tries to load Meteor before the mock is available, and you get a failure.

此外,流星模拟了 modules ,因此在注册模拟时,必须提供一个 module 名称,而不是变量名.

Moreover, Meteor mocks modules so when you register a mock, you have to give a module name, not the name of a variable.

这篇关于模拟模块不存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆