Posted by Vincent Woo
Fri, 05 Dec 2008 01:01:00 GMT
Upgrading a Rails project to version 2.2 broke controller specs and I couldn’t figure out why. The problem i was facing was that the default Rails exception rescuing would not work in the test environment.
I’m seeing alot of something like the following with ‘rake spec’:
ActiveRecord::RecordNotFound in 'PagesController responding to GET show should not find the record'
ActiveRecord::RecordNotFound
An exception is raised during the page request as expected but it prematurely ends the spec instead of going through the rescue handlers I’ve setup through rescue_from(). I had no idea if this new behavior is expected or a regression. Sure I could do…
lambda{do_get}.should raise_error(ActiveRecord::RecordNotFound)
...but I wasn’t looking forward to updating the spec suite to conform. Also, it wouldn’t be testing the rescue handler behaviors. Initially, searching via the almighty Google yield no answers. Stepping through code execution using rdebug revealed that RSpec modifies Rails to not rescue exceptions as it normally would. However, the modification also includes a simple way to revert to the default behavior. Score! So now I’m doing:
before(:each) do
controller.use_rails_error_handling!
end
With that solved, I now know which search terms to use and, lo and behold, Google reveals that somebody had the same problem and already figured it out. Google, why had you forsaken me earlier?
Also, there is more context if you’re interested.
Posted in Ruby on Rails | Tags exception, rails, rescue, rspec | no comments | no trackbacks
Posted by Vincent Woo
Tue, 15 Apr 2008 21:53:00 GMT
Since I’ve been following Jamis Buck’s advice to limit the brittleness of view tests, development has been much more productive. Test the things that matter most else you’ll end up with diminishing returns and the week passed by without any productive work done.
I’m taking one step further and I’ve decided not to write RSpec view specifications.
I will still continue to occasionally test important view elements. It just isn’t worth the extra baggage of writing and maintaining another set of specs for every single view. I’m feeling the pain there and I’m taking steps to eliminating it.
But it is also painful to sprinkle “integrate_views” syntactic vinegar throughout controller specifications. I don’t want to do without that since I would still get notified of template errors. Unfortunately, this is not the recommended behavior and not much was written on the subject.
Peter Marklund has a solution but it didn’t work for me. Maybe because RSpec was updated since his blog post was written but it is more likely that I’m missing something obvious. However, I dug around the plugin and was happy to discover a simple method override that worked. Add to spec_helper.rb:
module Spec
module Rails
module Example
class ControllerExampleGroup
module ControllerInstanceMethods
def integrate_views?
true
end
end
end
end
end
end
btw, while browsing the RSpec mailing list, I came across a post that explains a way to slightly speed up test runs.
Posted in Ruby on Rails | Tags override, rails, rspec, tests | no comments | no trackbacks
Posted by Vincent Woo
Sat, 19 Jan 2008 03:04:00 GMT
I’m in the middle of converting tests from Test::Unit to RSpec in an older Rails project. So far so good although I’m sure that would change as I dive into more complicated test cases. Still, I’m hoping the pain is worth it for the multiple contexts and lessened fixture reliance.
A nifty feature that I accidentally stumbled upon is that the default rake task would run the test in both test and spec directories. It’s not like I have to exclusively choose one or the other.
That opens up lots of test conversion options. I could convert the tests all at once or a little at a time. Or I could forget about conversion completely and merely write new tests in RSpec.
It’s little surprises like this that brightens up my workday.
UPDATE (FEB8,08): RSpec tests won’t run unless all Test::Unit tests passes.
Posted in Ruby on Rails | Tags bdd, rails, rake, rspec, test | no comments | no trackbacks
Posted by Vincent Woo
Sat, 19 Aug 2006 22:27:00 GMT
It was recently incorporated into Rails proper so it seemed like a good time to dive into the Simply Restful functionality. Not much documentation exists for it but many blogs have already posted tutorials.
The results of the past day or two’s tinkering distilled into a single 8.5 X 11 sheet:

.png
.pdf
References
Ryan’s Scraps (1)
Ryan’s Scraps (2)
scottraymondnet
Riding Rails
Meta|ateM
anthonyeden.com
Posted in Ruby on Rails | Tags cheatsheet, mime, rails, restful, routes | no comments | no trackbacks