arunoda / nodemock

Simple Yet Powerful Mocking Framework for NodeJs
http://arunoda.me
MIT License
75 stars 1 forks source link

No way to mock multiply-called method with differing return values #4

Closed rjkroege closed 13 years ago

rjkroege commented 13 years ago

In the current version, there is no way to return different values from a single multiply-called mock. Example:

mocked.mock('getBlah').return('one');
mocked.mock('setBlah').takes(2);
mocked.mock('getBlah').return('two');

// ...test stub...

test.equal(mocked.getBlah(), 'one', 'should be one');
mocked.setBlah(2);
test.equal(mocked.getBlah(), 'two', 'should be two');   // << fails --- getBlah returns 'one' again.

test.ok(mocked.assert(), 'bad/wrong mock order');
rjkroege commented 13 years ago

I wrote a possible patch for this issue and added a pull request for it.

arunoda commented 13 years ago

I agree with this. This works well if we calls the mock in order. In NodeJS or with Ajax calls we might not be able to have a order of execution. That might be the worst case and we cannot address that. Do you have any say?

rjkroege commented 13 years ago

Hi,

On Sunday, June 12, 2011 at 10:34 PM, arunoda wrote:

I agree with this. This works well if we calls the mock in order. In NodeJS or with Ajax calls we might not be able to have a order of execution. That might be the worst case and we cannot address that. Do you have any say?

Sure. I think that a test author can want to verify two different properties with a method like mock.assert(): all indicated mock methods are executed the specified number of times but their order does not matter all indicated mock methods are executed in precisely the order specified

Further, one might want to group mocks so that the test can assert that some sub-set of mock methods are executed in the defined order while the others just need to have been called.

Since I writing mocks for a state-rich object with many side effects (DOM), order of calls mattered and I badly wanted to assert that they were being invoked in the correct order.

So, maybe the API could be augmented. Here's a suggestion:

var mock = nm.mock("foo").takes(1).startorderblock(); mock.mock("bar").takes(2); mock.mock("bar").takes(3).endorderblock(); mock.mock("foo").takes("yo");

mock.assert() would be true for: foo("yo"), foo(1), bar(2), bar(3) or foo(1), bar(2), bar(3), foo("yo")

Rob.

arunoda commented 13 years ago

I think the current way is tolerable and we'll see any issues from others?

:)

On Mon, Jun 13, 2011 at 4:54 PM, rjkroege < reply@reply.github.com>wrote:

Hi,

On Sunday, June 12, 2011 at 10:34 PM, arunoda wrote:

I agree with this. This works well if we calls the mock in order. In NodeJS or with Ajax calls we might not be able to have a order of execution. That might be the worst case and we cannot address that. Do you have any say?

Sure. I think that a test author can want to verify two different properties with a method like mock.assert(): all indicated mock methods are executed the specified number of times but their order does not matter all indicated mock methods are executed in precisely the order specified

Further, one might want to group mocks so that the test can assert that some sub-set of mock methods are executed in the defined order while the others just need to have been called.

Since I writing mocks for a state-rich object with many side effects (DOM), order of calls mattered and I badly wanted to assert that they were being invoked in the correct order.

So, maybe the API could be augmented. Here's a suggestion:

var mock = nm.mock("foo").takes(1).startorderblock(); mock.mock("bar").takes(2); mock.mock("bar").takes(3).endorderblock(); mock.mock("foo").takes("yo");

mock.assert() would be true for: foo("yo"), foo(1), bar(2), bar(3) or foo(1), bar(2), bar(3), foo("yo")

Rob.

Reply to this email directly or view it on GitHub: https://github.com/arunoda/nodemock/issues/4#issuecomment-1358283

Arunoda Susiripala http://arunoda.com http://twitter.com/arunoda

rjkroege commented 13 years ago

On Monday, June 13, 2011 at 9:56 AM, arunoda wrote:

I think the current way is tolerable and we'll see any issues from others?

Sounds good to me.

Rob.

:)

On Mon, Jun 13, 2011 at 4:54 PM, rjkroege < reply@reply.github.com (mailto:reply@reply.github.com)>wrote:

Hi,

On Sunday, June 12, 2011 at 10:34 PM, arunoda wrote:

I agree with this. This works well if we calls the mock in order. In NodeJS or with Ajax calls we might not be able to have a order of execution. That might be the worst case and we cannot address that. Do you have any say?

Sure. I think that a test author can want to verify two different properties with a method like mock.assert(): all indicated mock methods are executed the specified number of times but their order does not matter all indicated mock methods are executed in precisely the order specified

Further, one might want to group mocks so that the test can assert that some sub-set of mock methods are executed in the defined order while the others just need to have been called.

Since I writing mocks for a state-rich object with many side effects (DOM), order of calls mattered and I badly wanted to assert that they were being invoked in the correct order.

So, maybe the API could be augmented. Here's a suggestion:

var mock = nm.mock("foo").takes(1).startorderblock(); mock.mock("bar").takes(2); mock.mock("bar").takes(3).endorderblock(); mock.mock("foo").takes("yo");

mock.assert() would be true for: foo("yo"), foo(1), bar(2), bar(3) or foo(1), bar(2), bar(3), foo("yo")

Rob.

Reply to this email directly or view it on GitHub: https://github.com/arunoda/nodemock/issues/4#issuecomment-1358283

Arunoda Susiripala http://arunoda.com http://twitter.com/arunoda

Reply to this email directly or view it on GitHub: https://github.com/arunoda/nodemock/issues/4#issuecomment-1358970

arunoda commented 13 years ago

cool.