Question

I have a member of my model that has these validations

validates :status, :presence => true, :numericality => {:only_integer => true}

In order to test that, my plan was to make a fixture that just has a perfectly valid instance of the model, and then do a test to make sure that was valid, another test to make sure setting game_status to nil was invalid, and 3 tests for the numericality: one that tests that it's invalid if i set it to a string, one that tests that it's invalid if i set it to a decimal, and one that tests that it's valid if i set it to an integer.

Overall, 5 tests to test 2 pieces of simple rails validation feels like i might be going overboard, especially when i add another member just like status that requires almost the same validations. I know rspec has things like should_validate_presence_of, but for my first project I want to use test::unit.

If that's the normal way to do things in rails, I'm fine doing it. But if there's a simpler way I'd rather do that. Or do you typically not test such simple things?

I'm not looking for a gem to abstract everything away for this, unless doing that is pretty much the accepted standard. Otherwise, I'd like to just use basic test::unit in a way that conforms to general rails best practices.

Was it helpful?

Solution

I'd say if you want those features to work then you should test them. Consider, for instance, if the Rails implementation changes in the future. A failing unit test would be a great way to immediately let you know "Hey! The behavior of my app has changed!"

That said, only test the cases you care about for your application—no need to duplicate the entire Rails test suite. Definitely have a test for your "happy" cases where the input should be considered valid. If you think it's possible/likely that a decimal or string might come through, then test what would happen in those cases as well. If you find yourself doing this in other places, then make yourself some shared helper methods like check_doesnt_allow_decimals, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top