Integration Testing w/ Specific Data

Hello Guys,

I’m looking for some advice on integration/unit tests which returns different results based on the arguments passed in.

One simple use case for this would be a login process. I need to pass a username and a password in to the login method, but I want to test several different scenarios.

  1. Attempting with valid credentials takes me to the welcome page.
  2. Attempting without populating the form sends me back to the login event and gives me a validation error.
  1. Attempting to login with false credentials passes me back to the login event with a failed login message.
  2. Attempting to login to an account which has been marked as locked, passes me back to the login event with a failed login message.

Now, in theory for this to work I have to have examples of each of those sets of credentials hard coded into my tests, and know that the data in the database which matches them exists.

I find myself coming across use cases similar to this, and at the moment I just don’t see a clean way of handling them.

I’d appreciate your advice.

Robert

Mock Objects will work here.

Regards,

Andrew Scott

http://www.andyscott.id.au/

HI Andew,

i had guessed that this would be a good use case for Mocking.

I'm very new to all this though. Could you elaborate a little, using my use case and explain which objects I would mock?

Thanks,

Robert

This is an older post that is for ColdBox 2.6.4, but this should get you started, as well as the Docs on the Wiki as well.

http://www.andyscott.id.au/2009/11/27/ColdFusion-unit-testing-and-using-MockBox

Regards,

Andrew Scott

http://www.andyscott.id.au/

I get an error trying to go to that link:

An unhandled exception has occurred. Please look at the diagnostic
information below:
Type uuid
Message The ID argument passed to the getEntry function is not of
type uuid.
Detail If the component name is specified as a type of this argument,
it is possible that either a definition file for the component cannot
be found or is not accessible.

Thanks,
Cory

http://www.andyscott.id.au/2009/11/27/ColdFusion-unit-testing-and-using-MockBox

- Gabriel

http://www.andyscott.id.au/2009/11/27/ColdFusion-unit-testing-and-using-Mock
Box

Does your email client split this?

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

From: coldbox@googlegroups.com [mailto:coldbox@googlegroups.com] On
Behalf Of Cory Miller
Sent: Friday, 8 April 2011 4:01 AM
To: ColdBox Platform
Subject: [coldbox:9265] Re: Integration Testing w/ Specific Data

I get an error trying to go to that link:

An unhandled exception has occurred. Please look at the diagnostic
information below:
Type uuid
Message The ID argument passed to the getEntry function is not of
type uuid.
Detail If the component name is specified as a type of this

argument,

it is possible that either a definition file for the component cannot be

found

The link up top doesn't have 'Box' at the end (I am not looking at
this through email - I am on the web).

Gabriel's link worked for me.

Thanks,

-Cory

Hi Andrew,

I’ve been playing with Mocking in unit tests for the past couple of days and gotten a good handle on things now.

From an integration point of view, using the use case I gave above, what exactly would you mock?

I assume we only really want to mock the data interaction layer? So that the controller is playing with the real life service layer, so we’re actually testing the integration?

Thanks,

Robert

Robert,

The way to think about this is put yourself into the shoes of writing the test first, you usually start off with something small and start building it up as you go along. So as soon as there is an external reference to what you are writing, then a mock is needed.

The idea behind this is that you begin writing the one section of code that you are interested in, and you don’t get bogged down later with having to maintain and write 2 tests at a time.

That is probably the easiest way to explain it.

Regards,

Andrew Scott

http://www.andyscott.id.au/

Robert,

The way to think about this is put yourself into the shoes of writing the test first, you usually start off with something small and start building it up as you go along. So as soon as there is an external reference to what you are writing, then a mock is needed.

The idea behind this is that you begin writing the one section of code that you are interested in, and you don’t get bogged down later with having to maintain and write 2 tests at a time.

That is probably the easiest way to explain it.

Regards,

Andrew Scott

http://www.andyscott.id.au/

Unit testing is the testing of a single unit of code, usually a method
belonging to a class whether it be a controller, service or model. In
those instances, you generally want to mock out all dependencies so
that the only thing you are testing are the internals of the method
itself. Given (mocked) input X does the method do Y as expected? Given
(mocked) input Q does the method throw error Z as expected.

When you are testing how different units work together, that is
usually considered integration testing. You are testing a cascading
flow of interactions then. So you might use Selenium or some sort of
tool and say "When user clicks this button, am I redirected to this
page?" and by doing this behavioral testing you are really testing all
the parts of the system that connect together to exhibit that behavior
(GET calls Controller A which fires Event B which invokes Service C
which calls Models E,F,G which returns that data that ends up as a
redirect to url Z)

Judah

No mocking is to deal with the unknown there and then.

And yes I do, unit tests are about 100% code coverage.

Have you ever seen this?

http://www.andyscott.id.au/2011/1/26/Where-are-my-mock-objects-you-lazy-son-of-

Regards,

Andrew Scott

http://www.andyscott.id.au/

Hey Judah,

Thanks for the info.

I understand the differences between Unit and Integration tests.

My question is, that in a use case such as:

  1. Form submitted with username / password.
  2. Login handler passes credentials to security service.
  3. Security service uses member service to validate credentials.
  4. Member service queries the database to find a user, returns boolean if found or not.
  5. Security service returns boolean value back to handler.
  6. Handler evaluates the boolean and redirects user appropriately.

In this scenario, the expected behaviour of the test depends on whether values passed in at the beginning, match data in the database.

So, how should I handle this?

  1. Hard code a username / password in the test, and ensure that I have matching details in the database?

or

  1. Mock the call to the database to simulate the behaviour.

Thanks,

Robert

Haha that’s great Andrew, I love those Hitler parodies. :smiley:

Could I ask for an example of a unit test for a controller? Do you have a link, or perhaps a code snip you’re willing to share? So I can see how you check that the next event is set as expected etc, accessing flash and so forth?

I think it’d help me understand that side of things a little better.

Thanks bud,

Robert

You use the test to pass the form variables, and you mock anything that is called externally to that function you’re testing.

Regards,

Andrew Scott

http://www.andyscott.id.au/

I use this as found in the Wiki for unit testing event handlers.

var event = “”;

//Place any variables on the form or URL scope to test the handler.

//FORM.name = “luis”

event = execute(“general.doSomething”);

//Do your asserts below for setnextevent you can test for a setnextevent boolean flag

assertEqualsString(“general.dspHome”, event.getValue(“setnextevent”,""), “Relocation Test”);

Regards,

Andrew Scott

http://www.andyscott.id.au/

O.k thanks Andrew,

That's how I've been kicking off my Integration tests at the moment, so I'll take the same approach with unit tests, but mock external dependancies.

Thanks,

Robert

Hi Andrew,

I'm talking about integration testing, so would I still want to mock all the external dependancies?

Robert

As in using something like Selenium? Then no that is different type of testing, that is automated user testing.

Regards,

Andrew Scott

http://www.andyscott.id.au/