Beginning Integration Testing - Need Starting Point

I will try to keep this as short and blunt as possible but this may
become to large of a question for one post. Here is where I am at:

1) I am trying to get started with integration testing using Coldbox
and MXUnit - I have all necessary files and everything set up
correctly - did a very basic test and MXUnit indicated a success.

2) Now I am trying to do a real world test against my app but I am
getting confused on what I need to mock, what I shouldn't mock, and
what I should ignore. The goal of integration testing, from my
understanding, should be to test the event (handler method) from start
to finish, ensuring all dependencies play nicely and do what they are
supposed to (so here is one of my assumptions: this should include
simulating all onXX (appInit, requestStart, etc...), interceptors, and
any other request dependent code). To make this simple, I only need
to simulate onAppInit and onRequestStart - no interception points.

Here is the code:

Haven't seen any responses so should be doing something different to
ask the questions in this post? The main reason I posted this is I
haven't seen anywhere a complete, real world, example which steps
through what you should and shouldn't do when trying to get started
with integration testing in Coldbox. In most real-world apps, there
is quite a bit going on to even bring up a simple view (if you require
authentication to access said view). I think it would be invaluable
to have a 'Integration Testing 101' type posting for n00bs like me. I
have went through the different tutorials and beginners guides to the
different areas (mocking, stubbing, handler testing, model testing,
etc...). DI is a pretty big source of confusion for me in regards to
handler testing - do I mock the injected dependencies? Where would I
do this (would I need to mock the handler? For example - if I wanted
to mock the userService in Main.cfc (to automatically authorize the
user) how would I do this?).

Please let me know if there is something else I should be doing to get
responses to this.

Thanks,
Cory

Cory,

If you want to listen to a couple of good screen casts that can help you
learn about mocking, etc I have listed some below. Some talk about
testing within ColdBox and others testing itself and using Test Driven
Development.

Just Mock It Part I by Luis Majano
http://adobechats.adobe.acrobat.com/p48673631/
Just Mock It Part II by Luis Majano
http://adobechats.adobe.acrobat.com/p52153979/
TDD Demystified: Get Started with TDD by Ryan Anklam & Jon Dowdle
http://experts.adobeconnect.com/p54641845/

This will hopefully help you.

Curt Gratz

Curt those screencasts were invaluable.

I am still having trouble wrapping my head around this though:

To test any of my handlers, there is a call to onRequestStart() in
Main.cfc. This assumes a user is authenticated before running the
handler - here is the code:

<cfcomponent extends="coldbox.system.EventHandler"
output="false"singleton="true">

        <cfproperty
name="userService"inject="model:ORMServiceFactory:getUserService"
scope="variables" />
        <cfproperty name="securityService"
inject="model:SecurityService"scope="variables" />

        <cffunction name="onRequestStart" returntype="void"
output="false">
                <cfargument name="event" required="true">

                <cfscript>
                var rc = event.getCollection();
                var prc = event.getCollection(private=true);
                var user = securityService.getUserSession();
                var key = "";
                var logger = logBox.getLogger(this);
                prc.httprequest = getHTTPRequestData();
                prc.cgi = cgi;
                prc.sidenav = true;
                prc.nav_active='';

                rc.xehLogout = "admin.user.doLogout";

                if(user.isAuthorized() AND !IsNull(user.getStore()))
                        rc.store = user.getStore();

                grabOCMVariables(prc);

                </cfscript>
        </cffunction>

</cfcomponent>

So as you can see var user = securityService.getUserSession() grabs
the user session (set when user logs in) - then:

f(user.isAuthorized() AND !IsNull(user.getStore()))
                        prc.store = user.getStore();

sets prc.store for use in all handlers (this is where I'm having
trouble). Now I could easily do an ORMExecuteQuery in the setup()
method but I really would like to get this working.

So here is where I am not sure what to do - should I mock the Main.cfc
handler and mock all injected services, methods that are used,
etc...? Is this the route to go?

Thanks,
Cory

Hi Cory,

Well, on request start and all preprocess executions are already taken care of by using of the ‘execute()’ method in the testing proxy.
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

I guess the basis of this question resides in the fact that an
authorized user is required to be 'logged in' (var user =
securityService.getUserSession()) for rc.store to be set (inside
onRequestStart()) - which is where I'm having trouble (as it appears I
need to simulate a login function first or something - which isn't
part of the event flow for this handler).

Does this make sense? Basically - how can I simulate
securityService.getUserSession() returns an authenticated user inside
onRequestStart(), therefore allowing rc.store to be set without
mocking and without editing my Main.cfc?

Thanks,
Cory

I would look at tahat code and in my setup make sure I have a valid
logged in user to test with,