Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 77fd2f7b24
Fetching contributors…

Cannot retrieve contributors at this time

executable file 114 lines (89 sloc) 3.192 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
require "spec_helper"

class User
  include ActiveModel::Validations
  include Humanizer
  require_human_on :create
end

describe Humanizer do
  
  before(:each) do
    @user = User.new
  end
    
  context "when mixed-in with a class" do
    
    it "adds questions and answers to the instances" do
      questions = @user.send(:humanizer_questions)
      questions.count.should == 2
      questions[0]["question"].should == "Two plus two?"
      questions[0]["answers"].should == ["4", "four"]
      questions[1]["question"].should == "Jack and Jill went up the..."
      questions[1]["answer"].should == "hill"
    end
    
  end
  
  context "question" do
    
    context "id" do
      
      it "is a random index for the questions array" do
        Kernel.should_receive(:rand).with(2).and_return(1)
        @user.humanizer_question_id.should == 1
      end
      
    end
    
    it "is retrieved based on the set id" do
      @user.should_receive(:humanizer_question_id).and_return(0)
      @user.humanizer_question.should == "Two plus two?"
      @user.should_receive(:humanizer_question_id).and_return(1)
      @user.humanizer_question.should == "Jack and Jill went up the..."
    end
    
  end
  
  context "answer" do
    
    it "is retrieved for a given id" do
      answers_for_id_0 = @user.send(:humanizer_answers_for_id, 0)
      answers_for_id_1 = @user.send(:humanizer_answers_for_id, 1)
      answers_for_id_0.count.should == 2
      answers_for_id_0.should include("4")
      answers_for_id_0.should include("four")
      answers_for_id_1.should == ["hill"]
    end
    
  end
  
  context "correct answer" do
    
    it "can be any of the answers" do
      @user.humanizer_question_id = 0
      @user.humanizer_answer = "4"
      @user.humanizer_correct_answer?.should be_true
      @user.humanizer_answer = "four"
      @user.humanizer_correct_answer?.should be_true
    end
    
    it "is case-insensitive" do
      @user.humanizer_question_id = 1
      @user.humanizer_answer = "HILL"
      @user.humanizer_correct_answer?.should be_true
      @user.humanizer_answer = "hiLL"
      @user.humanizer_correct_answer?.should be_true
    end
    
    it "cannot be nil" do
      @user.humanizer_question_id = 0
      @user.humanizer_answer = nil
      @user.humanizer_correct_answer?.should be_false
    end
    
    it "cannot be an answer that doesn't match" do
      @user.humanizer_question_id = 1
      @user.humanizer_answer = "slope"
      @user.humanizer_correct_answer?.should be_false
    end
    
  end

  context "when real human" do
    it "should pass the validations when correct answer" do
      @user.humanizer_question_id = 1
      @user.humanizer_answer = "HILL"
      @user.humanizer_correct_answer?.should be_true
      @user.humanizer_answer = "hiLL"
      @user.humanizer_correct_answer?.should be_true
      @user.valid?
      @user.errors.blank?.should be_true
    end

    it "should pass the validations when wrong answer" do
      @user.humanizer_question_id = 1
      @user.humanizer_answer = "slope"
      @user.humanizer_correct_answer?.should be_false
      @user.valid?
      @user.errors.blank?.should be_true
    end
    
  end

end
Something went wrong with that request. Please try again.