I wish to create tests for those my CRUD's. But how do you set another database on their behalf? Is the fact that the easiest method to go?
This really is another question, but it's related: Must I run the tests within the production server too? Sometimes things will go wrong in various enviroments, and so i guess I ought to. However I want the pointed out separate database, right?
Running any type of tests on the production server generally is a bad idea (unless of course it is simply the development hardware that has not been commisioned yet).
One Test doesn't hit the database (or other exterior system). So, to be able to produce a unit test you have to take away the reliance upon the database.
What you're calling a 'unit test' is most likely an integration test. Any test that utilises an exterior system (like a database, file system etc.) is definitely an integration test.
Two common methods to your condition are:
At the beginning of your test, restore a database backup that contains known data to some separate test database, then perform your results against it.
Utilizing a 'fixed' known test database, at the beginning of each test start a transaction, carry out the make sure then rollback the transaction to leave the database within the same known condition.
(No. 1 is frequently more suitable, because the database in (2) may become 'polluted').
To be sure with Mitch. I'd add that you ought to decide whether for you to do an integration test or perhaps a unit test (or both, although not within the same test). If, actually, you need to do wish to perform a unit test, realize:
- Your code includes a "dependency" with an exterior database.
- When unit testing you will have to find away out to "fake" the database. You need to test a "unit" meaning just one factor, not several things (i.e. your CRUD code As well as your link with a database And also the database itself).
- Typically you will need to refactor your code using something similar to dependency injection to ensure that when unit testing you are able to fake stuff that your code is dependent on.
Unit testing is not just testing your code. That's really the simple part. Greater part is making your code testable.
I suggest likely to http://artofunittesting.com/ and watching the free videos around the right side underneath the heading "Unit Testing Videos". Forget the truth that he's employed in .Internet as it is the concepts which are important.
Then watch the GoogleTechTalks by Misko Hevery where he describes why for you to do dependency injection.
(He's more too. There's a series of six GoogleTechTalks.)
I'd an identical problem today and i believe I have found a great choice.
- Create a copy of the database (developing a new empty database works too).
- Edit your config_test.yml to alter the database title.
An example of my test configuration (may be different depending for those who have multiple db-s etc)
doctrine: dbal: dbname: test_db
- Improve your database to mirror the organizations inside your application by calling
php app/console doctrine:schema:update --force --env=test(needed, should you just produced a brand new db in addition to any time you improve your application model).
The application should are now using the exam database throughout unit tests. NB! Make sure to create a backup of the database before playing around using the live database.
However, as clearly pointed out before, these aren't Unit Tests any longer and rather an integration tests.