Posted by Rick DeNatale
Tue, 09 Oct 2007 12:15:00 GMT
I recently got assimilated by the Mac/TextMate borg. I'm slowly teaching my fingers to dance the TextMate tango and unlearning old vim habits.
One resource has been James Edward Gray II's book on TextMate published by the Pragmatic Programmers. I finally sat down and got serious about writing some automations of my own.
I've got to say that I'm pretty impressed by TextMate. At the last Raleigh.rb hack night I was talking to another vim user. I'd mentioned to him that you can extend TextMate easily in Ruby, without really having experienced it. Today, I wrote a neat little TextMate command to help in building Rails database test fixtures.
It acts like a tab triggered snippet, but it's a smart little critter. If I'm editing a fixture file, say 'test/fixtures/users.yml', I can type item then tab and it will produce the skeleton yaml for a new record, with:
- A dummy name selected for overtyping.
- The id set to the next available primary key
Each column name as a yaml key ...
- ... A tabstop on a value which shows the column type
For example:
Read more...
Posted in rails | Tags automation, fixtures, testing, textmate | 1 comment | no trackbacks
Posted by Rick DeNatale
Thu, 19 Jul 2007 18:19:00 GMT

Yesterday I wrote about some code I wrote to control what Time.now and Date.today return to support time-dependent testing.
This afternoon I discovered a critical, but easy to fix bug. I’ve updated the original article.
The good news is that the testcase for the time machine now has another test!
Posted in rails | Tags culpas, mea, testing | no comments | no trackbacks
Posted by Rick DeNatale
Wed, 18 Jul 2007 19:45:00 GMT

I’ve been working on adding support for localization of the user’s time zone on an existing Rails app for a client. In order to test this, I found myself building a time machine.
At first, I did a fairly simple hack which monkey patched the system methods Time.now, and Date.today. This worked until I got into some testcases of code which was triggered off of updated_at fields in various ActiveRecord models. Since I had to deal with these implicit dates, I found that I needed to have finer control than my simple patch gave.
The code has now evolved to the point where I think that it shows some interesting aspects of basic Ruby metaprogramming.
Read more...
Posted in ruby, rails | Tags metaprogramming, testing | 1 comment | 1 trackback
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