How to use plugins inside test environment (mxunit) to test model

Hi all,

I'm struggling a little with understanding the different base test
classes in coldbox.system.testing - the naming indicates the intended
purpose (eg BaseModelTest), however in trying to get my own model
tests working I'm struggling to understand best way to integrate some
of the coldbox bits I need.

For example -- I have a Service class that I'm trying to test. It has
several dependencies which are injected via annotations. Among them
are:
a) gateway
b) plugin

My test CFC extends BaseModelTest and I can mock the gateway and
inject it -- so far so good. When it comes to the plugin, however, I
can't work out how to instantiate it in order to inject it.

I need the plugin injected and working as I can't mock its output in a
useful fashion (since the output of the service method depends on the
output of the plugin). I assumed I would be able to call
getController.getPlugin("blah")

How do I get a handle on Coldbox to grab a plugin?

getController() returns 0 and on looking at BaseModelTest, its setup
method does not call super.setup() -- I presume this is by design
since model objects don't have direct access to coldbox.

Also is the Application.cfc (for test) meant to extend Coldbox? Should
it more or less mirror the application.cfc for the main app that it's
testing? I'd love some clarification on how to set up the
environment!

cheers
CC

Try getPlugin() without the getController() I use getModel() in this way so
I am assuming the same for getPlugin()

You can always do debug(this) to see all methods in mxUnit debug output as
well.

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

From: coldbox@googlegroups.com [mailto:coldbox@googlegroups.com] On
Behalf Of oobi
Sent: Wednesday, 2 March 2011 11:51 AM
To: ColdBox Platform
Subject: [coldbox:8472] How to use plugins inside test environment

(mxunit)

to test model

Hi all,

I'm struggling a little with understanding the different base test classes

in

coldbox.system.testing - the naming indicates the intended purpose (eg
BaseModelTest), however in trying to get my own model tests working I'm
struggling to understand best way to integrate some of the coldbox bits I
need.

For example -- I have a Service class that I'm trying to test. It has

several

dependencies which are injected via annotations. Among them
are:
a) gateway
b) plugin

My test CFC extends BaseModelTest and I can mock the gateway and inject
it -- so far so good. When it comes to the plugin, however, I can't work

out

how to instantiate it in order to inject it.

I need the plugin injected and working as I can't mock its output in a

useful

fashion (since the output of the service method depends on the output of
the plugin). I assumed I would be able to call
getController.getPlugin("blah")

How do I get a handle on Coldbox to grab a plugin?

getController() returns 0 and on looking at BaseModelTest, its setup

method

does not call super.setup() -- I presume this is by design since model

objects

don't have direct access to coldbox.

Also is the Application.cfc (for test) meant to extend Coldbox? Should it

more

or less mirror the application.cfc for the main app that it's testing?

I'd love

You Will need to mock the plugin. I believe the base test has a grtmockplugin method that can build it for you.

Thanks for that -- getting closer...

The plugin is created and injected, now but I think I'm still missing
some pieces

The test fails when it attempts to call this method on the plugin:
beanFactory.getModel("SitemapBean").init(argumentCollection=arguments);

The error is "key [COMPATMODE] doesn't exist in struct"
line 81 - BeanFactory.cfc

81 if( instance.compatMode ){
82 return
instance.beanFactory.getModel(argumentCollection=arguments);
83 }

From looking in the beanfactory it appears to be calling getModel,
which I assume means that Coldbox needs to be loaded (since
SitemapBean is aliased in ModelMappings.cfm). My test extends
BaseModelTest which does not load Coldbox. I tried extending
BaseTestCase instead but the error is the same.

It appears as if the plugin isn't initialised -- does it need to be
manually initialised after using getMockPlugin()? If so how?

thanks again.
CC

If you are mocking everything, you need to mock that call, if not you are mocking half way. If you are just testing your model separately, mock all of its dependencies.

Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

To mock that call (as I understand it) I'd need to create the object
that the BeanFactory is meant to return, inject all of its
dependencies (and in turn any of the dependencies used by the
dependencies etc)... seems like a fair bit of messing about.

The service method I'm trying to test basically receives a query from
the gateway, does a bit of manipulation and then creates a bean (using
BeanFactory) and populates it using data from the query. The bean
itself has some injected dependencies.

My test mocks the result from the gateway (using QuerySim). What I was
hoping to do was just analyse the completed bean using asserts. If I
mock the result from the beanfactory then it seems like I'm
invalidating the test -- or am I approaching this the wrong way?

Also what happens if I needed to retrieve more than one bean from the
BeanFactory? If I've mocked its getModel method then it'll return only
the one thing...

Apologies if I'm being dense - I'm not very familiar with the
methodology. I'd love to see an example of a service/gateway test (as
opposed to the bundled handler tests) if anyone has one!

sorry for the bump -- does anyone have a good sample test for a
gateway/service that they'd care to share? I'm struggling a little
with the concepts more so than the code...