Tag: testing

Use Hash#except to easily test Rails validations

Ruby on Rails provides a large API for web development, including adding new methods to Ruby’s base classes. One method I don’t see used that often is Hash#except. From the Rails API documentation: Return a hash that includes everything but the given keys. This is useful for limiting a set of parameters to everything but …

Read more

Correcting your rcov report for untested code

I’ve used rcov for a long time to check the code coverage on my Ruby applications. Code coverage shows what parts of an application have not been tested. It can be abused if it’s used as an absolute metric (must be 99%+) but it’s great to see where fragile parts might be located. A problem …

Read more

Testing exceptions in Rails

If you want to make sure an exception is thrown in a Rails test just use the assert_raise assertion. For example, I am testing to make sure the SQL statement is invalid after it goes through my model’s validation def test_exception c = Content.new # Title is missing assert_raise ActiveRecord::StatementInvalid do c.save end end This …

Read more