4

I'm trying to understand the use of fixtures in the context of unit testing models in a web MVC system.

The problem I am trying to understand is that fixture data does no give a true representation of the data that the system accepts.

A simple example is say I have a user model that has email validation. Fixtures will bypass the validation so the tests maybe testing invalid data.

Edit: What I'm trying to get at is, wouldn't it make more sense to create the test data through the application business logic?

So for the email example, if a developer accidentally created a fixture with an email that would not pass validation, this could cause false negatives in testing (eg testing sending emails would fail because the fixture has invalid data).

The email example probably isn't the best, but I hope that makes more sense what I'm trying to get at?

3
  • 1
    Why would you want to unit-test a model? You probably should better be testing your validation.
    – mhr
    Commented Feb 6, 2014 at 10:09
  • related: What are the different meanings of 'fixture'?
    – gnat
    Commented Feb 6, 2014 at 10:14
  • fixtures are hardcoded data, they are under your control and they are test helpers... if you write valid fixtures or invalid fixtures, it depends on what you want to test
    – SparK
    Commented Feb 6, 2014 at 13:37

2 Answers 2

5

in the context of unit testing

wouldn't it make more sense to create the test data through the application business logic?

In some cases, that might be a more useful test to write. But be aware you have moved out of the realm of unit testing and into integration testing. Both are necessary.

if a developer accidentally created a fixture with an email that would not pass validation, this could cause false negatives

I would argue you should intentionally create invalid test input, and test that your unit handles it appropriately. If you do this for every unit, your code becomes more robust.

In other words, testing against invalid data forces you to code defensively, which is a good thing.

1
  • I think I'm getting confused between unit and integration testing, my understanding, a lot of web frameworks tend to combine them.
    – Rowan
    Commented Feb 6, 2014 at 21:34
1

Tests that pass invalid data to some code under test tell you whether that code reacts properly to that data.

For example, when a method that doesn't specifically need to accept null parameters receives one, an IllegalArgumentException should be thrown immediately to prevent less-obvious failure later on. Even if this code is behind an interface that prevents those null values from propagating into the code under test, it doesn't hurt to verify the exception behavior.

In many web applications, data is validated in Javascript before it gets submitted to the server, however that Javascript can be circumvented, so validation (and validating testing) must be included in the server-side code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.