Rails, Unit Tests and setup not working
No posts in a while ... there's a reason for that (very exciting for me, maybe not so much for you, dear reader) - I'll be telling you my news soon enough.
In the meantime, many thanks to the good folks at the North West Ruby User Group mailing list (I don't live in the North West but it certainly seems to be a hotbed of Rails action) for helping me out with this one.
It started simply enough. Another day, another controller another functional test.
fixtures :thingies, :wotsits
def setup
super
@thingy = thingies :my_thingy
end
def test_something
get :something
assert_response :success
assert_equal @thingy.name, assigns(:page_title)
assert_equal @thingy, assigns(:thingy)
end
Open Terminal, type ruby test/functional/thingy_controller_test.rb. Runs fine. All tests pass.
Happy developer.
A bit later, just before committing my changes, type rake test:functionals. Fails. No such method on nil. Nil? Nil? Where does nil come from?
Try running the test on its own? Works fine. Try it in rake? Fails.
I then edited it and added some puts statements in each test and in the setup method. Ran the test on its own - all my text appears as expected. Ran rake - all my text appears except the text from setup. So setup is not being called? But why?
Well, I never found out why but the NWRUG members pinpointed the problem. It was the call to super in setup - apparently there is no method in the super-class and under certain circumstances it never seems to return from this call to the non-existent method. Remove the super and everything runs fine.
Still no explanation as to why it works one way and not the other but at least it works.
No comments:
Post a Comment