Testing a Plugin

Hi all,

I need to be able to test a plugin which needs some information from
the event. Namely the current module.

I can't for the life of me work out how I can test this plugin, and
mock the current event without actually running the event as I just
want to unit test the plugin and this one function.

Any pointers gratefully received.

Cheers

Kev

Good question Kev, I'd be interested on ideas for this too.

I currently use an integration tests to hit handler methods to test it.

Rob

I would try to create an instance of “coldbox.system.web.Context.RequestContext” using MockBox and mock the result you need to test. But, I don’t think ColdBox will check the type of object you pass, so, you actually can probably get away with just using a stub"

MockBox = new coldbox.system.testing.MockBox();

SUT = new plugin_to_test();

MockEventObject = MockBox.createStub(false);

MockEventObject.$(‘getCurrentModule’,‘foobar_value_to_assert’);

SUT.doSomethingCool(event=MockEventObject);

Something like this from the What's new should help, if you need more info
please let me know.

<cfcomponent extends="coldbox.system.testing.BasePluginTest"
plugin="coldbox.system.plugins.HTMLHelper">
<cfscript>
function testaddAssetJS(){
  var mockEvent = getMockRequestContext();
  mockRequestService.$("getContext", mockEvent);
  
  // mock the plugin's htmlhead method
  plugin.$("$htmlhead");
  
  // Call method to test
  plugin.addAsset('test.js,luis.js');
  
  debug( plugin.$callLog().$htmlhead);
  
  // test duplicate call
  assertEquals('<script src="test.js"
type="text/javascript"></script><script src="luis.js"
type="text/javascript"></script>' , plugin.$callLog().$htmlhead[1][1] );
  plugin.addAsset('test.js');
  assertEquals(1, arrayLen(plugin.$callLog().$htmlHead) );
}

function testTableORM(){
  data = entityLoad("User");
  
  str = plugin.table(data=data,includes="firstName");
  debug(str);
  
assertEquals('<table><thead><tr><th>firstName</th></tr></thead><tbody><tr><t

Joe</td></tr><tr><td>Luis</td></tr></tbody></table>',str);

}
</cfscript>
</cfcomponent>

Regards,
Andrew Scott
http://www.andyscott.id.au/

Andrew hit it on the spot. You now have targeted tests for each part of ColdBox: plugin, model, handler, etc. In the what’s new you can find more information.

Luis