arunoda / nodemock

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

Added a callhook method #15

Closed tristanls closed 11 years ago

tristanls commented 12 years ago

This is to enable executing code in testing environment after mock is triggered.

Http response use case:

var res = new EventEmitter();
var returnData = function (call) {
  call(); // callback fired
  res.emit( 'data', ... ); // start feeding the response
  //...
  res.emit( 'end' );
};
httpMock.mock( 'request' ).takes( {
  host : 'host',
  port : 80,
  path : '/',
  method : 'GET'
}, function () {} ).returns( reqMock ).callhook( returnData ).calls( 1, [res] );

Trigger assertion asynchronously use case:

var res = new EventEmitter();
var verify = function (call) {
  httpMock.assert();
  done(); // some async framework test finish
};
httpMock.mock( 'request' ).takes( {
  host : 'host',
  port : 80,
  path : '/',
  method : 'GET'
}, function () {} ).returns( reqMock ).callhook( verify ).calls( 1, [res] );
arunoda commented 12 years ago

Sorry for the late reply.

This is quite usefull. But I'm quite not happy with the naming and the use of this. How about following syntax instead of this?

function hookme() {}
nodemock.mock('foo').takes('aa', 'bb', function() {}).calls(2, ["yes"]).hook(hookme);

This explains what we trying do. What do you think?

tristanls commented 12 years ago

Hey,

From what I understand, you're proposing a name change from callhook to hook? Sure, that works. The order of the call is flexible already right? I think it works both ways, so that should work as well.

Cheers

arunoda commented 12 years ago

Yes. That's true. You are correct.

On Mon, Apr 9, 2012 at 3:23 AM, tristanls < reply@reply.github.com

wrote:

Hey,

From what I understand, you're proposing a name change from callhook to hook? Sure, that works. The order of the call is flexible already right? I think it works both ways, so that should work as well.

Cheers


Reply to this email directly or view it on GitHub: https://github.com/arunoda/nodemock/pull/15#issuecomment-5018718

Arunoda Susiripala

@arunoda http://twitter.com/arunoda http://gplus.to/arunodahttps://github.com/arunoda http://www.linkedin.com/in/arunoda

kgilpin commented 12 years ago

I think the ctrl option already provides this? Or am I missing something?

arunoda commented 12 years ago

No It's different. ctrl() can be used to trigger the callback at anytime within your code. default is, it will be called at the moment where the mocked method is called.

But hook() can be used to execute some code inside the mocked function.

I know this can be hook is not needed always and it's use cases can be implemented with some other design using the same set of functions. But having hook() is okay. there may have some use :)

tristanls commented 11 years ago

I'm no longer supporting this pull request.