Skip to content

Instantly share code, notes, and snippets.

@jedschneider
Created April 12, 2010 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedschneider/363886 to your computer and use it in GitHub Desktop.
Save jedschneider/363886 to your computer and use it in GitHub Desktop.
class ScreenerTest < ActiveSupport::TestCase
context "a screener" do
context "yes/no questions" do
setup {@screener = Factory.build(:screener, :answers => [Factory(:answer)])}
should "not allow blank answers for yes/no questions" do
assert_equal false, @screener.valid?
end
should "define the error message for invalidly answered yes/no questions" do
@screener.save
assert_equal "All yes/no answers must be answered yes or no",
@screener.errors.full_messages.to_sentence
end
should "not give any trouble to good answers to yes/no questions" do
assert_equal true, Factory.build(:screener, :answers => [Factory(:answer, :value => 1)]).valid?
end
end
end
end
class Screener < ActiveRecord::Base
def validate
return unless new_record?
if answers and answers.find_all do |answer|
answer.question.response_type == "yes/no" and answer.value.blank?
end.size > 0
errors.add_to_base("All yes/no answers must be answered yes or no")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment