Chris Bloom
unread,Jun 27, 2011, 4:39:34 AM6/27/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message as abuse
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to factory_girl
It appears that when one has a *required* boolean attribute field, one
is unable to set that value to `false` through FactoryGirl, as FG
seems to think that means "don't set it"
# app/models/news_article.rb
class NewsArticle < ActiveRecord::Base
attr_accessible :title, :content, :published_at, :published
# ...
validates :published,
:presence => true
end
# spec/factories.rb
Factory.define :news_article do |f|
# ...
f.published true
end
# spec/models/news_article_spec.rb
describe NewsArticle do
describe "default scope" do
before do
NewsArticle.delete_all
t = Time.local(2004, 02, 25, 13, 30, 0)
Timecop.freeze(t)
@published_in_30_days = Factory(:news_article, :published_at
=> (Date.today + 30), :published => true)
@unpublished_in_31_days = Factory(:news_article, :published_at
=> (Date.today + 31), :published => false)
end
it "should only show published articles with published_at dates in
the past" do
# ...
end
end
end
# After running `rspec spec/` from Terminal
NewsArticle default scope should only show published articles with
published_at dates in the past
Failure/Error: @unpublished_in_31_days =
Factory(:news_article, :published_at => (Date.today + 31), :published
=> false)
ActiveRecord::RecordInvalid:
Validation failed: Published can't be blank