Rails Functional Testing Gotcha
I just started writing my first real Rails app this weekend, and as I struggle along I thought I'd document where I'm tripping up. My first order of business is to write functional tests on my controllers and views. For some reason I thought I'd give the tests (which inherit from ActionController::TestCase) descriptive class names like PropertiesDashboardTest instead of PropertiesControllerTest. I had tests running and passing before I renamed the test class, but then started getting this error:
RuntimeError: @controller is nil: make sure you set it in your test's setup method.
It took me a while to realize that Rails auto-instantiates a controller instance based on the test name when you inherit from ActionController::TestCase. Since I don't have a controller named PropertiesDashboard.rb, Rails can't set one up in the test for me. Of course I'm doing this test-first, so a controller doesn't even exist at this point. That also explains why my existing test started failing when I renamed it.