0

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 won't build on x86 platforms

2) Node-jasmine won't build on 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.

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');

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

pedalpete
  • 21,076
  • 45
  • 128
  • 239
  • I don't have node on my Raspi atm. Can you tell me what `console.log('This platform is ' + process.platform);` returns? You can hopefully use that to conditionally set `wpi`. I've had good luck with [sinonjs](http://sinonjs.org/) for mocking. – PeterVC Apr 29 '14 at 04:19
  • Also check `require('os').arch()` and `require('os').platform()`. – PeterVC Apr 29 '14 at 04:27
  • Just installed node and wiring-pi. Looks like `var wpi = require('os').arch() === 'arm' ? require('wiring-pi') : {};` would be a good start. – PeterVC Apr 29 '14 at 05:01
  • Thanks PeterVC, this is what I was suggesting a question a few days ago, but didn't get a response as to if this is a good idea or not. I was thinking that if 'wiring-pi' doesn't exist, I'll mock the objects directly in the getters file. I wasn't sure if this was good practice though. – pedalpete Apr 29 '14 at 05:04
  • How much do you have going on in `getter.js`? Can you isolate all of the `wiring-pi` logic so that you can abstract it away? – PeterVC Apr 29 '14 at 05:50
  • I've got enough going on in the getter that I want to have good tests for it. I'm just struggling with isolating the wiring-pi logic so I can run the tests, but it isn't going that well. I may have to do some refactoring so that I can put each wiring-pi method into its own holder method, then in my tests I can just make sure the wiring-pi method is called with the correct values. I won't worry about what it returns (separation of concerns I think). If you supply everything you've said here as an answer, I'll accept it. Thanks for you help. – pedalpete Apr 29 '14 at 05:54
  • As far as Jasmine goes I am able to install and run it. Are you using Wheezy? Which version? – PeterVC Apr 29 '14 at 14:49

1 Answers1

1

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

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

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

PeterVC
  • 4,574
  • 2
  • 16
  • 14
  • sorry for the delay in replying, I corrupted my SD card and had to rebuild. The error I get when trying to install jasmine-node unfortunately isn't very helpful, it just said 'failed to fetch from registry: jasmine-node', but all my other modules install just fine. – pedalpete May 03 '14 at 07:55
  • Hopefully http://stackoverflow.com/questions/12913141/installing-from-npm-fails can help. – PeterVC May 03 '14 at 14:30