Skip to content

Instantly share code, notes, and snippets.

Created Oct 17, 2012
What would you like to do?
Example of desired behaviour of SinonJS onlyWithArgs() method
beforeEach(function() {
global.fs = require('fs');
sinon.stub(fs, 'readFileSync').onlyWithArgs('my-file.txt').returns('Contents of file');
// Then require the module under test, which uses fs.readFileSync() internally
// require() uses original method with correct calling context
global.myModule = require('../src/my-module');
it('does something with the file', function() {
expect(myModule.loadFromFile()).toEqual('Contents of file');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment