#187
Nov 09, 2009

Testing Exceptions

Sometimes bad code slips into production and triggers a 500 error. Learn how to be notified of this and resolve it through integration tests.
Download (14.4 MB, 10:01)
alternative download for iPod & Apple TV (11.2 MB, 10:01)

Resources

script/generate integration_test exceptions
rake test:integration
# test/integration/exceptions_test.rb
class ExceptionsTest < ActionController::IntegrationTest
  fixtures :all

  test "POST /products" do
    post "/products", "commit"=>"Submit", "product"=>{"name"=>"Headphones", "price"=>"-2"}
    assert_response :success
  end
  
  test "GET /products/8/edit" do
    product = Product.first
    get "/products/#{product.id}/edit"
    assert_response :success
  end
end

RSS Feed for Episode Comments 17 comments

1. Joost Saanen Nov 09, 2009 at 01:38

thx for the cast!


2. Ryan Nov 09, 2009 at 01:50

Thanks for this. We're pushing a site into production soon, and this looks like it will help us cover a part of our tests.

Thanks again!


3. Jakub Arnold Nov 09, 2009 at 01:59

This is exactly what I was looking for :) great job


4. Robert Nov 09, 2009 at 02:43

Thanks, definitely useful stuff.


5. vojto Nov 09, 2009 at 03:26

What generated that nice list of exceptions? That one in file "untitled 1496" ?


6. Matthew Savage Nov 09, 2009 at 04:34

Ahh, to get the trace you would run the rake task with --trace, right?

I haven't tried this though..


7. Benjamin Lewis Nov 09, 2009 at 05:04

Thanks Ryan

Keep up the good work.


8. Todd Nov 09, 2009 at 07:27

suggestion if comment.include?('UGG')
 then move to spam


9. Joseph Silvashy Nov 09, 2009 at 08:28

Ryan, you need to do something about the spam. I think you should disable all links that aren't to pastie, github, or the similar.


10. Nico Nov 09, 2009 at 08:58

Wonder why the captcha does not prevent that spam. I just replaced my old captcha (simple_captcha rails plugin) with the recaptcha used here, in the hope that it will work better...


11. Ryan Bates Nov 09, 2009 at 09:47

Thanks for the feedback guys. I'm planning to deal with the spam problem soon. Captcha doesn't stop it because it's actual humans spamming AFAIK.


12. Cezar Nov 09, 2009 at 11:15

Thanks Ryan, nice screencast. Please post more TDD/BDD videos, I can't seem to get enough of those :)

Thanks again!


13. Ryan Bates Nov 09, 2009 at 11:59

@Jamie, unfortunately it's not so simple. I hate to blanket delete based on words in the comments. For example "he boots up the app" and "I learned Ruby with the Shoes framework" are both valid comments. I'll consider doing it for "ugg" though.


14. John Topley Nov 09, 2009 at 12:09

Damn, looks like I'll have to rename my forthcoming "Ugg" Gem then! ;-)


15. Scott Johnson Nov 09, 2009 at 15:25

At the very least Ryan please disable comments for the older screencasts. New comments on those seem to be about 99% spam.


16. pulkit Nov 09, 2009 at 22:47

Can you stop spams by locating its IP address and to block it if it is coming from limited number of computers?

I think it is possible that it is coming from limited number of computes because almost all spams looks same talking about boots and shoes!


17. Richard Nov 10, 2009 at 03:26

Thanks as always for your amazing screen-casts. Is the full stack-trace not available in the testing log file? That is where I usually look for errors during testing...

Cheers


18. John Nov 10, 2009 at 05:23

Excellent screencast, although nothing of this is new to me :)

One thing I was wondering is why you made integration tests; they seem to be functional tests to me. And, as Matthew Savage said, I think you can get the full trace by running 'rake test:integration --trace'.


19. tilthouse Nov 10, 2009 at 13:53

I wanted to note that in the iTunes podcast feed, both this episode and the previous one (#187, #186) generate errors when trying to sync with my iPhone. No other Railscast on that feed does. It's the standard "wrong format" message.


20. Giacomo Nov 11, 2009 at 01:11

Thanks for all your screencasts, greet work.
If you want to access the exception inside your test you can write:

@response.template.instance_variable_get(:@exception)
(Look at the source code for "assert_response" inside ActionPack.)

Since the assertion accept a message as second parameter you can pass the backtrace there:

assert_response :success,
  @response.template.
  instance_variable_get(:@exception).
  backtrace.join("\n")

or put it in a method to cleanup the backtrace, ecc.

Thanks again.

Giacomo


21. Giacomo Nov 11, 2009 at 08:13

Correcting myself (comment #37):
when no exception is raised (hopefully most of the time) the code calls #backtrace on a nil object. Here a corrected version:

http://pastie.org/693810

Giacomo


22. myliverhatesme Nov 11, 2009 at 10:55

How about making it so after x number of people report a comment as spam, it automatically gets hidden. Then you can permanently delete it later if you wish or unhide it if it really isn't spam.


23. Tobias Nov 14, 2009 at 07:56

Thanks for the great screencast again!

About the annoying Spam-Problem:

Please try using Akismet.
http://akismet.com/
It works great on thousands of WordPress sites. Without any annoying captcha.

http://github.com/jfrench/rakismet might be a place to start (not used yet).

Regards


24. Chris K Nov 17, 2009 at 20:28

I have a solution for the spam. Since most of us know at least a bit about rails (otherwise we wouldn't give a rat's ... about the Railscasts), as a simple question in addition to Captcha for example:

Fill in the blank:

validates_xxxxx_of :firstname, :lastname

or something more funny (and political LOL):

validates_xxxxx_xxx :smartpresidents, :in => "George W. Bush"


25. Jason Rudolph Nov 21, 2009 at 13:35

Another excellent screencast. Thanks, Ryan.

Tarantula can help here as well.

http://github.com/relevance/tarantula

As you know, many errors like this can be caught just by clicking through the site and inputting both valid and invalid data on every form. But none of us want to do that manually, since it's both a bore and a huge chore. However, Tarantula will happily do the job for you. "Tarantula is a big fuzzy spider. It crawls your Rails application, fuzzing data to see what breaks."

Tarantula will crawl your entire app, and report any 500s or 404s it encounters. It only takes a few minutes to add it to your Rails app. And if you're not already using it, I'll bet it will find at least one issue you didn't know about. And then you can fix it *before* it affects one of your users!

Using Tarantula is not an excuse to avoid writing good integration tests, but it is another valuable tool in your toolbox.


26. Francois Nov 23, 2009 at 11:01

thanks for another great screencast Ryan. Railscasts has become a wonderful reference guide for me. When I'm tackling a given a problem I often remember an episode that covered that topic and I go back and watch the relevant episode again.


27. Singapore Ecommerce Mar 18, 2010 at 11:07

Thank you...


28. Singapore Drupal Modules Mar 18, 2010 at 11:10

I totally agree,

Is this article a joke “Chrome the new IE6″. I design in chrome and it almost always seems to work for me.

This has incorrect things in it “Chrome doesn’t support HTML5″ The one problem is that my whole website is in HTML5 and it looks great in chrome.


29. mp4 players Apr 05, 2010 at 01:12

I have been trying to learn as much about marketing, not just internet marketing, and I have found some very good info here. Great site thank you.


30. dior store May 24, 2010 at 18:47

every good!!


31. rowing machine Jul 17, 2010 at 00:42

This is exactly what I was looking for,thanks for sharing. Keep up the good work.



33. iPhone Ringtone Maker for mac Jul 20, 2010 at 18:47

this is a best ,ah ah you can have a try


34. timberlandbootsuk Aug 02, 2010 at 02:07

we provide our buyers with an efficient and manageable procurement process covering every phase of the international supply chain and

streamlining trade channels. Also welcome wholesaling, feedback now!


35. acekard 2 Aug 02, 2010 at 20:16

I am glad to read some fantastic acekard 2i article like this.

http://www.ndscardstore.com/Acekard
http://www.ndscardstore.com/R4
http://www.ndscardstore.com/DSTT


36. <a href=" http://www.maxexercises.com" > Wholesale NHL jersey </a> Aug 03, 2010 at 02:58

it is really a great article. Thanks

<a href=" http://www.maxexercises.com" > Wholesale NHL jersey </a>
<a href=" http://www.maxexercises.com" > Cheap NHL jersey </a>
<a href=" http://www.maxexercises.com" > NHL jersey for sale</a>


37. free directory list Aug 11, 2010 at 22:38

I think this is very dangerous


38. skin care Aug 17, 2010 at 23:53

skincareinfo.com is an independent [url=http://www.skincareinfo.com]skin care[/url] information portal to help consumers discover:
Which [url=http://www.skincareinfo.com]skin care[/url] products really work How to avoid being misled by infomercials and advertising hype The positive and negative of various skin care treatments so you the consumer can make informative decisions
How to reduce your skin care expenses dramatically


39. jordan retro shoes Aug 18, 2010 at 20:59

Well done, Ryan. Great stuff. I really enjoyed reading this article. I will come back to check it out for update.


40. wholesale new era hats Aug 20, 2010 at 20:20

I recently came across your blog and have been reading along.
I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading.Nice blog,I will keep visiting this blog very often.


41. Wholesale Electronics Aug 25, 2010 at 01:26

Discount Wholesale Electronics, Wholesale Cell Phones, Electronic Gadgets and More from the Best Dropship Wholesaler


42. louis vuitton shoes Aug 26, 2010 at 21:02

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


43. snow boots Aug 30, 2010 at 20:42

I think it is possible that it is coming from limited number of computes because almost all spams looks same talking about boots and shoes!


44. louis vuitton sunglasses Sep 01, 2010 at 21:16

Wow this is a great resource.. I’m enjoying it.. good article

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source