18.12.2012 Views

Rails Magazine - Issue 3

Rails Magazine - Issue 3

Rails Magazine - Issue 3

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

34<br />

How to Implement Automated Testing by Eric Anderson<br />

34<br />

Declarative Code<br />

Another abuse of testing I often see, related to the 3rd<br />

party code issue, is testing declarative code. The previous<br />

code example was very declarative but let me give you a more<br />

subtle example.<br />

class Notifier < ActiveMailer::Base<br />

def refer_a_friend(to, sender, url)<br />

recipients to<br />

from sender<br />

subject ‘A referal from a friend’<br />

body :url => url<br />

end<br />

end<br />

Does the above code need automated testing? Probably<br />

not. However, if we added some logic to our method or email<br />

template, then the code becomes less declarative and more<br />

procedural. As the code does this it starts to become a good<br />

idea to add some automated tests to ensure your procedural<br />

code is behaving correctly.<br />

Markup Testing<br />

Often, developers will over-test an application’s output.<br />

This can make it very high maintenance to update without<br />

It's going to blow<br />

providing much benefit in return.<br />

A perfect example of this is the markup in your templates.<br />

Testing for certain key bits of markup in your generated<br />

templates can be useful to automatically determine if your<br />

templates are being generated correctly. But it’s a mistake to<br />

start testing too much markup, as it is very likely to change as<br />

the design of the application evolves.<br />

Your automated testing will never be able to see if the page<br />

“looks” right. So, by over-testing markup, all you’re doing is<br />

increasing maintenance costs when someone does want to<br />

make markup changes.<br />

Testing For Error Messages<br />

Another area where developers over-test is error messages.<br />

It’s a mistake to test for specific error messages. Error<br />

messages are very likely to get reworded so testing for the<br />

exact text will make your tests fragile.<br />

Instead, try to find a way to test that an error occurred and<br />

maybe what type of error without doing character by character<br />

comparison of the error messages. Perhaps you can check<br />

a error code or verify the exception thrown was a certain type<br />

of exception.<br />

For example, in a functional test of a controller you<br />

may want to make sure the flash[:warning] was set and not<br />

flash[:notice] when testing to ensure an error is processed<br />

correctly. The actual text of the flash message is less important.<br />

What is more important is that the correct type of flash<br />

message was set.<br />

Final Words<br />

It’s important not to be lured into thinking that because<br />

your tests pass, your application is bug-free. Absence of failure<br />

is not proof of perfection.<br />

Automated testing should be considered a first step. Make<br />

sure to use your application in a real browser and use it often.<br />

Setup your application to report errors to you. Tools like the<br />

exception_notifier plugin are extremely valuable because your<br />

automated testing is only going to test for the things you can<br />

guess might go wrong. But that is really just a small fraction<br />

of what can go wrong.<br />

In conclusion, automated testing is an excellent tool to<br />

speed up development and integration, avoid regression and<br />

gain confidence in your application’s correctness. But don’t<br />

fall in to the trap of blindly implementing thousands of tests<br />

just because someone says you are a bad developer if you<br />

don’t. Often 100% coverage is overkill and it can sometimes<br />

even be a detriment to your application. Instead, always consider<br />

your end goals when developing a test suite.<br />

Discuss: http://railsmagazine.com/3/9

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!