Oct 31 2009
∞
rspec, errors, and idiocy
Just spent ten minutes writing a test that should’ve taken thirty seconds. The final test looks like this:
it "should throw an Already Running error if there is already a server running" do
some_thing.should_receive(:some_method).and_raise(RuntimeError.new('no error'))
lambda {
some_thing.that_triggers_the_error()
}.should raise_error(RuntimeError, "some error")
end
The first mistake was mine. I wrote:
some_thing.should_receive(:some_method).and_throw(RuntimeError.new('some error'))
Too much javascript, I guess. But the crazy thing is, yes, it throws. You end up getting a NameError: uncaught throw for ‘some error”. Super confusing, right? Easy mistake to make, though.
The second issue is more of an rspec message shortcoming. I originally had:
.should raise_error(RuntimeError.new('some error'))
which resulted in the matcher error:
expected some error but nothing was raisedBut something *was* raised — it just wasn’t being matched correctly because I used the wrong matcher arguments. This is, yes, technically also my fault, but the unhelpful matcher error makes it difficult to catch. Anyways, hopefully this gotcha description finds its way into the hands of people who need it, since google wasn’t super helpful when I dropped in some key words.