最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does this Sinon mock have a mocked method that is not a function? - Stack Overflow

programmeradmin4浏览0评论

I would like to use test doubles in my coffeescript unit tests to help with separation of concerns.

I am using sinon with mocha (in the context of a Rails app with konacha.)

I am trying what at this point seems straight out of the documentation, which has this example of mock usage:

var myAPI = { method: function () {} };

var spy = sinon.spy();
var mock = sinon.mock(myAPI);
mock.expects("method").once().throws();

PubSub.subscribe("message", myAPI.method);
PubSub.subscribe("message", spy);
PubSub.publishSync("message", undefined);

mock.verify();
assert(spy.calledOnce);

In my case I'm trying to mock a function call on an object as follows:

canvas = sinon.mock getContext: (arg) ->
canvas.expects("getContext").once()
canvas.getContext('2d')
canvas.verify()

This gives a TypeError indicating that getContext is not a function:

TypeError: canvas.getContext is not a function

The mock seems to be setup and getting verified correctly. When omitting the call to getContext, I am informed that an expectation was not met:

ExpectationError: Expected getContext([...]) once (never called)

The piled JavaScript looks like this, then:

var canvas;

canvas = sinon.mock({
  getContext: function(arg) {}
});

canvas.expects("getContext").once();

canvas.getContext('2d');

canvas.verify();

What could account for this error?

I was wondering if I was doing something strange with the function argument, but I can reproduce this without an argument to the getContext call.

I would like to use test doubles in my coffeescript unit tests to help with separation of concerns.

I am using sinon with mocha (in the context of a Rails app with konacha.)

I am trying what at this point seems straight out of the documentation, which has this example of mock usage:

var myAPI = { method: function () {} };

var spy = sinon.spy();
var mock = sinon.mock(myAPI);
mock.expects("method").once().throws();

PubSub.subscribe("message", myAPI.method);
PubSub.subscribe("message", spy);
PubSub.publishSync("message", undefined);

mock.verify();
assert(spy.calledOnce);

In my case I'm trying to mock a function call on an object as follows:

canvas = sinon.mock getContext: (arg) ->
canvas.expects("getContext").once()
canvas.getContext('2d')
canvas.verify()

This gives a TypeError indicating that getContext is not a function:

TypeError: canvas.getContext is not a function

The mock seems to be setup and getting verified correctly. When omitting the call to getContext, I am informed that an expectation was not met:

ExpectationError: Expected getContext([...]) once (never called)

The piled JavaScript looks like this, then:

var canvas;

canvas = sinon.mock({
  getContext: function(arg) {}
});

canvas.expects("getContext").once();

canvas.getContext('2d');

canvas.verify();

What could account for this error?

I was wondering if I was doing something strange with the function argument, but I can reproduce this without an argument to the getContext call.

Share Improve this question asked Jul 4, 2015 at 15:57 Joseph WeissmanJoseph Weissman 5,7175 gold badges47 silver badges76 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

You are trying to call methods on the mock directly, but this isn't how Sinon.JS thinks of mocks. Consider the example code again:

var myAPI = { method: function () {} };

var spy = sinon.spy();
var mock = sinon.mock(myAPI);
mock.expects("method").once().throws();

PubSub.subscribe("message", myAPI.method);
PubSub.subscribe("message", spy);
PubSub.publishSync("message", undefined);

mock.verify();
assert(spy.calledOnce);

The subject under test is myAPI, not mock. In the case above, something like the following will work:

canvas_api = getContext: ->
canvas_mock = sinon.mock(canvas_api)
canvas_mock.expects("getContext").once()
canvas_api.getContext()
canvas_mock.verify()
发布评论

评论列表(0)

  1. 暂无评论