如何模拟/存根node.js模块 [英] how to mock/stub a node.js module
问题描述
我正在研究一些要在RaspberryPi上运行的代码,并且正在使用Wiring-Pi节点模块. 我有两个问题
I'm working on some code to run on RaspberryPi, and I'm using the Wiring-Pi node module. I have two problems
1)Wiring-Pi无法在x86平台上构建
1) Wiring-Pi won't build on x86 platforms
2)Node-jasmine无法在RaspberryPi上构建
2) Node-jasmine won't build on RaspberryPi
因此,在研究了许多不同的想法之后,我想知道我是否最好在x86平台上模拟或存根Wiring-Pi模块,以便我可以运行测试.
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.
当然,我的问题是我正在测试的文件包含require语句
Of course, my problem is that the file I'm testing includes the require statment
// getters.js
var wpi = require('wiring-pi');
// getters.spec.js
var get = require('../lib/getters.js');
有没有一种方法可以设置规范文件,以使getters.js文件不加载connectioning-pi模块?
Is there a way I can set-up the spec file so that the getters.js file doesn't load the wiring-pi module?
推荐答案
您可以使用它来控制wire-pi模块的加载:
You can use this to control loading of the wiring-pi module:
var wpi = require('os').arch() === 'arm' ? require('wiring-pi') : {};
Jasmine可以在Pi上编译,安装和运行.您遇到什么错误?
Jasmine compiles, installs and runs on the Pi. What error do you get?
这篇关于如何模拟/存根node.js模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!