3
ActiveRecord의 데이터베이스 열에 해당하는 메서드를 테스트하는 데 문제가 있습니다. 다음은 (내 경우 문서에)있어 어떤 모델을 가지고, 그리고 수행테스트 중? on ActiveRecord 클래스가 인스턴스화 될 때까지 작동하지 않습니다.
$ 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
분명히 그 모든 데이터베이스 열을 추가입니다) (.new를하기위한 전제 조건으로 일어나고 일부 액티브 라이프 사이클 작업이 클래스에 대한 메소드.
이것은 실제로 단위 테스트 중 일이 복잡합니다. 내가 런타임에 액티브 클래스를 허용하고 그들에 대한 몇 가지 유효성 검사를 수행하는 클래스를 가지고 싶습니다
예를 들어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
내가 (.new를 함께 정크 인스턴스를 만드는 것보다 (다른 주장 할 수있는 일)) ActiveRecord가 초기화를 완료 했습니까?
으로 문의 해 주셔서 감사합니다. '.attribute_method? '는 문제를 해결합니다! – phlegx