Posted by Rick DeNatale
Tue, 17 Jul 2007 17:50:00 GMT
The other day I wrote about
problems with undeclared fixtures in Rails ActiveRecord tests.
Here’s a little code snippet which might be useful in tracking down such problems.
Read more...
Posted in rails | Tags fixtures, testing | no comments | no trackbacks
Posted by Rick DeNatale
Sat, 14 Jul 2007 19:28:00 GMT
The other day I was working on adding support for user selected time-zones to an existing rails app for a client. As usual I was doing test-first development. One of the things that makes rails such a pleasure is that a good set of tests give confidence that you aren’t breaking “legacy” code.
I also use, and really like, the rails plugin for vim which does lots of nice things like making navigation between the files of rails apps much easier. It also has a nice feature which adds a :Rake command to vim which “does the right” thing contextually. For example if you are editing a migration and enter :Rake, it runs rake db:migrate. If you are in a test file it runs just the single test selected by the cursor, or just that test file if you aren’t positioned to a particular test.
I was doing the latter, and my test was failing, and I was having a hard time debugging it. I tried executing just that single test from the bash command line with:
$ruby test/unit/my_test.rb -n"test_mytest"
and it still failed, no surprise. The same thing happened if I ran the entire test file, in fact, other tests which had worked before were now failing, and I was really mystified now because I didn’t see how I had done anything which had a remote chance of breaking those.
So I figured I probably should test everthing so:
$rake test
And, surprise of surprises, but every test worked. Not only my failing unit tests, but the functional and integration tests as well.
To make a long story short, the problem turned out to be that the tests were failing because, now that the particular model was sensitive to the user’s timezone, it needed access to it’s associated user model, and I hadn’t told the testcase that the users fixture was needed. Running the testcase in isolation, the users table wasn’t being populated for the test case. But running rake test, or rake test:units meant that other tests run before had left the data I needed behind.
Just a little thing that makes fixtures less than ideal.
Posted in ruby, rails | Tags fixtures, gotchas, testing | no comments | no trackbacks
Posted by Rick DeNatale
Sat, 14 Jul 2007 19:10:00 GMT
Josh Susser just posted a new Rails plugin
which adds a validates_existence_of method to ActiveRecord::Base.. The idea is that beyond validates_presence_of, which simply checks that a foreign key field is non-nil, or validates_associated which
validates the model referenced by a foreign key, validate_existence ensures that the referenced model
exists in the database, no more no less.
It’s brand new, but it sure looks like a nice idea.
Posted in rails | Tags activerecord, referential_integrity | no comments | no trackbacks
Posted by Rick DeNatale
Mon, 02 Jul 2007 15:36:00 GMT
I’m working on a team rails project and adding some timezone support. I installed the tzinfo gem and, since I want to make sure that it gets to the production server, I used the freeze_other_gems rake task, which I’d found by doing a rake -T.
I’d never used it before so I googled to find some documentation.
It wasn’t exactly clear, but it turns out that you need to edit the fourth line of the lib/tasks/gems.rake file
libraries = %w(progressbar tzinfo)
The progressbar gem was already there so I added tzinfo.
Then I invoked:
Only to get the error:
rake aborted! undefined method `version’ for nil:NilClass
This problem had been seen by others and commented on. I guessed that the problem was not with my newly added gem, but the progressbar gem which was already listed, apparently by one of the other developers.
The Quick Fix
The first step was to determine the version of the progressbar gem. I looked at lib where it had been installed, and found that it was version 0.3. I then installed the gem, and reran the freeze_other_gems rake task.
Success
Posted in rails | Tags gems, rails, rake | no comments | no trackbacks