Unit Testing Doctrine ORM Models
I think this is quite a stupid question, but do you test your Models if you already use a ORM framework. I was at it when I realized there isn't much to test? I mainly will just use the models for values? eg. for adding a user
$user = new开发者_运维百科 User();
$user->username = "user1";
$user->password = "password";
$em->persist($user);
$em->flush();
then edit will be similar
$user = /* get user */
$user->email = "new@email.com";
$em->flush();
something like that. The only functionality that I will probably add is register user & change password to handle password salting & verification that username is available.
Maybe another thing is getters & setters? Quite a trivial thing? How will you do that?
public function testCanSetUsername() {
$user->username = "Hello";
$this->assertEquals("Hello", $this->username);
}
Just like that?
You should write story tests that ensure that the code does what the user wants. It sounds like a simple statement, but these are the most important tests. These really tests what the app does rather than how. You'll see that these tests end up covering the code you have in your question.
For example, you may have tests like:
- user registers to website
- user changes password
- etc.
In fact, if you write all these tests and there is code that is not covered by them, you probably don't need that piece of code, and would not have written it in the first place if you had used TDD.
精彩评论