Created
April 12, 2010 18:58
-
-
Save jedschneider/363886 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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