Coldbox - mxunit testing database interaction

2009-05-12 Thread Jake Pilgrim

I'm just starting to get into Coldbox and I'm encountering some confusion as to 
how to *really* test my models and database interaction. 

So I have the following handler function which is fired upon form submission:

cffunction name=editAction
cfargument name=event type=any
cfscript
var rc = arguments.event.getCollection();
var demoModelObj = getModel('demoModel');

demoModelObj.editUserObj(rc);

runEvent('demoApp.list');
/cfscript
/cffunction

... which calls the following model function:

cffunction name=editUserObj
cfargument name=rc type=struct required=true hint=request 
collection /
cfscript
if (structKeyExists(rc,'userID')) {
instance.usersObj.load(rc.userID);
instance.usersObj.setAll(rc);
instance.usersObj.save();
}
/cfscript
/cffunction


This all works fine and dandy (at least for a 'hello world' coldbox application 
- I know this code is lacking in a number of places). So now I'm trying to set 
up a mxunit test to test the behaviors of the handler, but I'm coming to a 
blank when I try to come up with an assertion for this... I could bundle the 
whole test up in a cftransaction, and rollback when I'm done - I get that, but 
the problem is I don't see how to test this w/o writing twice as much code 
within the test. Take the following beginning of a unit test:

cffunction name=testEditAction
cfargument name=event type=any
cfscript
var event = ;
var rc = '';

// set up fake form submission
rc.userID = 1;
rc.userName='testUser';
rc.password = 'testPass';
rc.departmentID_fk = 1;

event = execute(demoApp.edit);
rc = event.getCollection();
debug(rc);

// what to do here?
/cfscript
/cffunction

... what kind of assertions can I make at the 'what to do here?' line? Is there 
an easy way to get at instance.usersObj from the model's method, or do I have 
to have my model return it (seems wrong to have the model return just to allow 
for debugging)? Am I totally missing the short-bus here? Any insight would be 
greatly appreciated! 

~|
Want to reach the ColdFusion community with something they want? Let them know 
on the House of Fusion mailing lists
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:322453
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4


Re: Coldbox - mxunit testing database interaction

2009-05-12 Thread Matt Quackenbush

Jake,

I would highly recommend posting this question to the ColdBox and/or MXUnit
groups (links below).  You are almost guaranteed to get a much greater
response from those than you will from CF-Talk.

/0.02

I would offer a suggestion, but I'm honestly not exactly sure what you're
trying to do, or specifically, what you're struggling with.  But that might
be just because I a) don't use any of the built-in model stuff, and b) don't
really write controller unit tests.  I write extensive tests on my model
itself, but typically just browser test the controllers.

HTH


~|
Want to reach the ColdFusion community with something they want? Let them know 
on the House of Fusion mailing lists
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:322455
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4


Re: Coldbox - mxunit testing database interaction

2009-05-12 Thread Jake Pilgrim

Thanks for the response Matt. I'll repost this there. In the meantime for 
anyone else viewing this thread, what I'm struggling with is finding out if the 
save occurred successfully. Maybe the answer is that I should be unit testing 
the model directly and ignoring this test within the handler's unit tests. 
Here's what I'm thinking this unit test should look like (partial pseudo-code):

 cffunction name=testEditAction
cfargument name=event type=any
cfscript
var event = ;
var rc = '';
var testObj = '';

// set up fake form submission
rc.userID = 1;
rc.userName='testUser';
rc.password = 'testPass';
rc.departmentID_fk = 1;

event = execute(demoApp.edit);
rc = event.getCollection();
debug(rc);

testObj = createObject('component','path.to.users');
testObj.load(rc.userID);

assertEquals(testObj.get('username'),rc.username,'usernames are not equal.');

assertEquals(testObj.get('password'),rc.password,'passwords are not equal.');

/cfscript
/cffunction

... but you can see how this is a MUCH bigger function than what I'm actually 
trying to test. If I used this approach in a more complex scenario, I could see 
it taking twice as long to write the test than it did to write the 
functionality I'm trying to test... Seems like a losing battle - I'm hoping I'm 
missing something? 

Thanks!

Jake,

I would highly recommend posting this question to the ColdBox and/or MXUnit
groups (links below).  You are almost guaranteed to get a much greater
response from those than you will from CF-Talk.

/0.02

I would offer a suggestion, but I'm honestly not exactly sure what you're
trying to do, or specifically, what you're struggling with.  But that might
be just because I a) don't use any of the built-in model stuff, and b) don't
really write controller unit tests.  I write extensive tests on my model
itself, but typically just browser test the controllers.

HTH 

~|
Want to reach the ColdFusion community with something they want? Let them know 
on the House of Fusion mailing lists
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:322457
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4