内でフィールドが nil にならないように強制する際に問題が発生していますActiveAttr::Model
。
この制約をコントローラーで定義するのではなく、モデル内で強制するエレガントな方法はありますか? または、シナリオに対して間違ってテストしていますか?
モデル:
class Message
include ActiveAttr::Model
attribute :name, :presence => true, :allow_nil => false, :allow_blank => false
attribute :email, :presence => true
attribute :content, :presence => true
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
validates_length_of :content, :maximum => 500
end
R仕様:
module MessageSpecHelper
def valid_message_attributes
{
:name => "Walter White",
:email => "walter@hailtheking.com",
:content => "I am the one who knocks"
}
end
end
it "should have error on name (alternate with nil)" do
@message.attributes = valid_message_attributes.except(:name => nil)
@message.should have(1).error_on(:name)
end