I'm having a problem testing for methods that correspond to the database columns of an ActiveRecord. Take any model you've got (in my case Document), and do the following:
$ rails c
Loading development environment (Rails 3.0.9)
>> Document.method_defined?(:id)
=> false
>> Document.new
=> #<Document id: nil, feed_id: nil >
>> Document.method_defined?(:id)
=> true
There is obviously some ActiveRecord lifecycle work going on as a prerequisite to .new() that's adding all the database columns as methods to the class.
Where this really complicates things is during unit testing. I'd like have a class that in runtime accepts ActiveRecord classes and does some validation on them
For example
class Foo < ActiveRecord::Base
def work1
# something
end
end
class Command
def operates_on(clazz, method)
# unless I add a "clazz.new" first, method_defined? will fail
# on things like :id and :created_at
raise "not a good class #{clazz.name}" if ! clazz.method_defined?(method)
# <logic here>
end
end
describe Command do
it "should pass if given a good class" do
Command.new.operates_on(Foo,:work1)
end
it "should pass if given a database column" do
# this FAILS
Command.new.operates_on(Foo,:id)
end
it "should raise if given an invalid class/method combo" do
lambda { Command.new.operates_on(Foo,:non_work) }.should raise_error
end
end
What can I do to assert (other than making a junk instance with .new()) that the ActiveRecord has done its initialization?