7

I am working with process.platform and want to stub that string value to fake different OSes.

(this object is generated out of my reach, and I need to test against different values it can take on)

Is it possible to stub/fake this value?

I have tried the following without any luck:

stub = sinon.stub(process, "platform").returns("something")

I get the error TypeError: Attempted to wrap string property platform as function

The same thing happens if I try to use a mock like this:

mock = sinon.mock(process);
mock.expects("platform").returns("something");
Community
  • 1
  • 1
Automatico
  • 12,420
  • 9
  • 82
  • 110
  • This makes no sense to me. Anyone please correct me if I'm off base here. I normally use stubs to create fake functions/methods that are used by the function/method being tested in order to create a predictable scenario for the function/method being tested. I've never tested the returned value of a stubbed method, since (assuming that I'm forcing my stub method to return a value) I know what that value will be already. It makes even less sense to assign a value to an object property just to turn around and test it. Maybe I misunderstood your question. If so, please elaborate some more. – JME Jun 07 '15 at 05:56
  • So, maybe there is too little context here. What I am doing is making a cross-platform npm module which must act differently on different OS-es, thus I must stub/fake which operating system I am on. I have to stub `process.platform`, which I believe I can't change by simply overwriting it. – Automatico Jun 07 '15 at 06:23
  • Sure you can. I'll post it as my answer. – JME Jun 07 '15 at 06:41
  • 1
    @Cort3z your example code should reference `process.platform` instead of `global.generated_object`. Always show real code. – Mulan Jun 07 '15 at 06:51
  • @naomik: Updated question now :) Thanks for the feedback – Automatico Jun 07 '15 at 07:41

2 Answers2

11

You don't need Sinon to accomplish what you need. Although the process.platform process is not writable, it is configurable. So, you can temporarily redefine it and simply restore it when you're done testing.

Here's how I would do it:

var assert = require('assert');

describe('changing process.platform', function() {
  before(function() {
    // save original process.platform
    this.originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform');

    // redefine process.platform
    Object.defineProperty(process, 'platform', {
      value: 'any-platform'
    });
  });

  after(function() {
    // restore original process.platfork
    Object.defineProperty(process, 'platform', this.originalPlatform);
  });

  it('should have any-platform', function() {
    assert.equal(process.platform, 'any-platform');
  });
});
JME
  • 3,592
  • 17
  • 24
3

sinon's stub supports a "value" function to set the new value for a stub now:

sinon.stub(process, 'platform').value('ANOTHER_OS');
...
sinon.restore() // when you finish the mocking

For details please check from https://sinonjs.org/releases/latest/stubs/

Ron Wang
  • 119
  • 3