#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 43 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. cheap ugg Dec 15, 2009 at 23:19

Wholesale and retail are both acceptable to us. Welcome to our site and free to look! Thank you and wish you a

nice day. Good Luck!


28. Ed hardy Dec 17, 2009 at 23:41

Thanks for sharing.


29. yeni müzik Dec 29, 2009 at 15:46

This is one of those "clever" solutions that will only cause


30. Manolo Blahnik shoes Jan 24, 2010 at 23:04

Wholesale and retail are both acceptable to us. Welcome to our site and free to look! Thank you and wish you a

nice day. Good Luck!


31. cheap adidas shoes Jan 31, 2010 at 19:06

Adidas Shoes Online Shop-Hot Selling Adidas Shoes & Cheap Adidas Shoes.
per


32. Electronics Feb 05, 2010 at 06:40

Thanks for the great screencast again!


33. Mobile Phones Feb 21, 2010 at 04:44

thanks for another great screencast Ryan. Railscasts has become a wonderful reference guide for me


34. Electronic Cigarette Feb 21, 2010 at 04:47

Another excellent screencast. Thanks, Ryan.

Tarantula can help here as well.


35. Top Download movies Feb 22, 2010 at 08:08

I thought it was going to be some boring old post,


36. delta faucets Feb 28, 2010 at 01:40

thanks again.


37. high power flashlight Mar 01, 2010 at 20:01

Nowadays, you can get a high power LED flashlight of high quality with less than 25 dollars. It's truly both attractive in price and quality.


38. cheap Abercrombie Mar 02, 2010 at 23:15

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...


39. acai berry Mar 05, 2010 at 00:08

definitely useful stuff, great job!


40. berry acai Mar 05, 2010 at 00:16

Railscasts has become a wonderful reference guide for me. Thanks Ryan.


41. webcam girl Mar 05, 2010 at 00:36

excellent screencast. Thanks, Ryan.


42. kamagra Mar 09, 2010 at 01:37

thanks for the share


43. m65 field jacket Mar 09, 2010 at 01:37

nice post

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