Skip to content

Instantly share code, notes, and snippets.

@vokshirg
Created May 15, 2016 15:56
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 vokshirg/c57745c45cab0f41eac1789b19c46a86 to your computer and use it in GitHub Desktop.
Save vokshirg/c57745c45cab0f41eac1789b19c46a86 to your computer and use it in GitHub Desktop.
Fail with answer's specs
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :user
validates :body, :question_id, :user_id, presence: true
default_scope { order("right_answer DESC").order("created_at DESC") }
def is_right_answer
self.question.answers.where("right_answer = ?", true).update_all("right_answer = false")
self.update(right_answer: true)
end
def not_right_answer
self.update(right_answer: false)
end
end
context "executes methods correctly" do
context "#is_right_answer" do
before {
@qra = question_ra
@qra.answers.first.is_right_answer
@qra.answers.reload
@qra.reload
# raise @qra.answers.first.inspect
}
it { expect(question_ra.answers.first.right_answer).to eq true }
it { expect(question_ra.answers.last.right_answer).to eq false }
it "right answer of answer's question_ra is equal answer " do
# raise question_ra.inspect
expect(question_ra.right_answer).to eq(question_ra.answers.first)
end
end
end
Failures:
1) Answer public instance methods executes methods correctly #is_right_answer should eq true
Failure/Error: it { expect(question_ra.answers.first.right_answer).to eq true }
expected: true
got: false
(compared using ==)
# ./spec/models/answer_spec.rb:35:in `block (5 levels) in <top (required)>'
2) Answer public instance methods executes methods correctly #is_right_answer right answer of answer's question_ra is equal answer
Failure/Error: expect(question_ra.right_answer).to eq(question_ra.answers.first)
expected: #<Answer id: 1383, body: "Text of answer body #9", question_id: 736, created_at: "2016-05-15 15:54:58", updated_at: "2016-05-15 15:54:58", user_id: 2146, right_answer: false>
got: nil
(compared using ==)
# ./spec/models/answer_spec.rb:40:in `block (5 levels) in <top (required)>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment