Programing

Sinon.js로 클래스 메서드 스터 빙

lottogame 2020. 9. 11. 19:25
반응형

Sinon.js로 클래스 메서드 스터 빙


sinon.js를 사용하여 메서드를 스텁하려고하지만 다음 오류가 발생합니다.

Uncaught TypeError: Attempted to wrap undefined property sample_pressure as function

나는 또한이 질문 ( sinon.js에서 클래스를 스터 빙 및 / 또는 조롱합니까? ) 에 가서 코드를 복사하여 붙여 넣었지만 동일한 오류가 발생합니다.

내 코드는 다음과 같습니다.

Sensor = (function() {
  // A simple Sensor class

  // Constructor
  function Sensor(pressure) {
    this.pressure = pressure;
  }

  Sensor.prototype.sample_pressure = function() {
    return this.pressure;
  };

  return Sensor;

})();

// Doesn't work
var stub_sens = sinon.stub(Sensor, "sample_pressure").returns(0);

// Doesn't work
var stub_sens = sinon.stub(Sensor, "sample_pressure", function() {return 0});

// Never gets this far
console.log(stub_sens.sample_pressure());

위 코드에 대한 jsFiddle ( http://jsfiddle.net/pebreo/wyg5f/5/ )과 내가 언급 한 SO 질문에 대한 jsFiddle ( http://jsfiddle.net/pebreo/9mK5d/1/ ).

jsFiddle 및 jQuery 1.9 외부 리소스sinon을 포함했는지 확인했습니다 . 내가 뭘 잘못하고 있죠?


코드에서에서 함수를 스텁하려고 시도 Sensor하지만에서 함수를 정의했습니다 Sensor.prototype.

sinon.stub(Sensor, "sample_pressure", function() {return 0})

본질적으로 다음과 같습니다.

Sensor["sample_pressure"] = function() {return 0};

but it is smart enough to see that Sensor["sample_pressure"] doesn't exist.

So what you would want to do is something like these:

// Stub the prototype's function so that there is a spy on any new instance
// of Sensor that is created. Kind of overkill.
sinon.stub(Sensor.prototype, "sample_pressure").returns(0);

var sensor = new Sensor();
console.log(sensor.sample_pressure());

or

// Stub the function on a single instance of 'Sensor'.
var sensor = new Sensor();
sinon.stub(sensor, "sample_pressure").returns(0);

console.log(sensor.sample_pressure());

or

// Create a whole fake instance of 'Sensor' with none of the class's logic.
var sensor = sinon.createStubInstance(Sensor);
console.log(sensor.sample_pressure());

The top answer is deprecated. You should now use:

sinon.stub(YourClass.prototype, 'myMethod').callsFake(() => {
    return {}
})

Or for static methods:

sinon.stub(YourClass, 'myStaticMethod').callsFake(() => {
    return {}
})

Or for simple cases just use returns:

sinon.stub(YourClass.prototype, 'myMethod').returns({})

sinon.stub(YourClass, 'myStaticMethod').returns({})

Or if you want to stub a method for an instance:

const yourClassInstance = new YourClass();
sinon.stub(yourClassInstance, 'myMethod').returns({})

I ran into the same error trying to mock a method of a CoffeeScript class using Sinon.

Given a class like this:

class MyClass
  myMethod: ->
    # do stuff ...

You can replace its method with a spy this way:

mySpy = sinon.spy(MyClass.prototype, "myMethod")

# ...

assert.ok(mySpy.called)

Just replace spy with stub or mock as needed.

Note that you'll need to replace assert.ok with whatever assertion your testing framework has.


Thanks to @loganfsmyth for the tip. I was able to get the stub to work on an Ember class method like this:

sinon.stub(Foo.prototype.constructor, 'find').returns([foo, foo]);
expect(Foo.find()).to.have.length(2)

참고URL : https://stackoverflow.com/questions/21072016/stubbing-a-class-method-with-sinon-js

반응형