It might be time to call it a night:
So I’m testing out a location class in Rails (version 3!), and I want to make some comparison methods in the model, so I can do things like if location.empty? or location.match?(elsewhere) and so on, and it’s not working for me, so I crank out the unit tests that, yes, I should have written in the first place, and then…
and then…
I spend the next 20 minutes trying to figure out why I see this in the console:
>> ooo = Location.new()
=> #<Location id: nil, company_id: -1, edition: "", cross_street: "", business_name: "", telephone: "", address: "", city: "", postal_code: "", country: "Canada", created_at: "1900-01-01 00:00:00", updated_at: "1900-01-01 00:00:00>
Why was the country getting pre-populated? I’m using the Rails 3 beta, so my mind jumps to all kinds of weird conclusions. Yes, including how did they know where I live?
Anyway, as many of you already know, the table was defined thusly:
+---------------+--------------+------+-----+---------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+--------------+------+-----+---------------------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | company_id | int(11) | NO | MUL | -1 | | | edition | varchar(255) | NO | | | | | cross_street | varchar(255) | NO | | | | | business_name | varchar(255) | NO | | | | | telephone | varchar(255) | NO | | | | | address | varchar(255) | NO | | | | | city | varchar(255) | NO | | | | | postal_code | varchar(255) | NO | | | | | country | varchar(255) | NO | | Canada | | | created_at | datetime | NO | | 1900-01-01 00:00:00 | | | updated_at | datetime | NO | | 1900-01-01 00:00:00 | | +---------------+--------------+------+-----+---------------------+----------------+
…And yes, the ActiveRecord constructicon was simply taking the default value I’d provided.
(As is ever the case in these things, by the time I’d sorted out the test, things worked fine in my view. I’ve no idea what changed in the process, but anyway, it’s probably the best endorsement for unit testing I can find…)

