如何模拟/存根node.js模块

时间:2021-10-07 20:58:35

I'm working on some code to run on RaspberryPi, and I'm using the Wiring-Pi node module. I have two problems

我正在研究一些在RaspberryPi上运行的代码,我正在使用Wiring-Pi节点模块。我有两个问题

1) Wiring-Pi won't build on x86 platforms

1)Wiring-Pi不会在x86平台上构建

2) Node-jasmine won't build on RaspberryPi

2)Node-jasmine不会在RaspberryPi上构建

So, after playing around with a bunch of different ideas, I'm wondering if I'm best off to mock or stub the Wiring-Pi module when on x86 platforms, so I can run the tests.

所以,在玩了很多不同的想法之后,我想知道在x86平台上我是否最好模拟或存根Wiring-Pi模块,所以我可以运行测试。

Of course, my problem is that the file I'm testing includes the require statment

当然,我的问题是我正在测试的文件包含require语句

    // getters.js
    var wpi = require('wiring-pi');

    // getters.spec.js
   var get = require('../lib/getters.js');

Is there a way I can set-up the spec file so that the getters.js file doesn't load the wiring-pi module?

有没有办法设置spec文件,以便getters.js文件不加载wiring-pi模块?

1 个解决方案

#1


1  

You can use this to control loading of the wiring-pi module:

您可以使用它来控制布线-pi模块的加载:

var wpi = require('os').arch() === 'arm' ? require('wiring-pi') : {};

Jasmine compiles, installs and runs on the Pi. What error do you get?

Jasmine在Pi上编译,安装和运行。你得到什么错误?

#1


1  

You can use this to control loading of the wiring-pi module:

您可以使用它来控制布线-pi模块的加载:

var wpi = require('os').arch() === 'arm' ? require('wiring-pi') : {};

Jasmine compiles, installs and runs on the Pi. What error do you get?

Jasmine在Pi上编译,安装和运行。你得到什么错误?