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.
Attempting with valid credentials takes me to the welcome page.
Attempting without populating the form sends me back to the login event and gives me a validation error.
Attempting to login with false credentials passes me back to the login event with a failed login message.
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.
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.
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
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?
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.
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.
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)
Haha that’s great Andrew, I love those Hitler parodies.
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.