ruby on rails - Rspec validates_uniqueness_of test failing with additional validation errors -


i have 2 scoped uniqueness validations on answer model, trying test these using rspec , respective shoulda matchers.

as seen in test trace below, uniqueness validations message present in errors array however, there 2 , 3 other errors present; "body can't blank (nil)", "body short (minimum 10 characters) (nil)", "user_id can't blank (nil)". i'm not sure coming from, body , user attributes set explicitly in before block.

how can correct these additional errors uniqueness test pass?

answer.rb

    validates_uniqueness_of :correct, scope: :question_id, if: :correct?, message: "you can have 1 correct answer per question"     validates_uniqueness_of :user_id, scope: :question_id, message: "only 1 answer per question per user"      /* omitted brevity */ 

answer_spec.rb

require "spec_helper"  describe answer     before(:each)     @user1 = create(:user)     @answer = create(:answer, correct: true, user_id: @user1.id, body: "some text on 10 chars long")   end    subject { @answer }    { should respond_to(:user_id) }   { should respond_to(:question_id) }   { should respond_to(:body) }   { should respond_to(:correct)}   { should respond_to(:votes_count)}   { should respond_to(:points)}   { should belong_to(:question)}   { should belong_to(:user)}   { should have_many(:activities)}   { should have_many(:comments)}   { should have_many(:votes)}   { should validate_uniqueness_of(:correct).scoped_to(:question_id).with_message("correct can have 1 correct answer per question (true)") }   { should validate_uniqueness_of(:user_id).scoped_to(:question_id).with_message("user_id 1 answer per question per user (1)") }  /* omitted brevity */ 

test trace

  1) answer       failure/error: { should validate_uniqueness_of(:correct).scoped_to(:question_id).with_message("correct can have 1 correct answer per question (true)") }        expected errors include "correct can have 1 correct answer per question (true)" when correct set true, got errors: ["body can't blank (nil)", "body short (minimum 10 characters) (nil)", "user_id can't blank (nil)", "correct can have 1 correct answer per question (true)"]      # ./spec/models/answer_spec.rb:20:in `block (2 levels) in <top (required)>'    2) answer       failure/error: { should validate_uniqueness_of(:user_id).scoped_to(:question_id).with_message("user_id 1 answer per question per user (1)") }        expected errors include "user_id 1 answer per question per user (1)" when user_id set 1, got errors: ["body can't blank (nil)", "body short (minimum 10 characters) (nil)", "user_id 1 answer per question per user (1)"]      # ./spec/models/answer_spec.rb:21:in `block (2 levels) in <top (required)>'  finished in 1.31 seconds 17 examples, 2 failures 

factory.rb

factory :answer     user     question_id :question     body "you need change grip"     votes_count 0     correct false   end 

it's failing because you're not testing @answer. you're not defining subject in these tests. it's using rspec's subject default going new instance of whatever class you're describing, ie. answer.new. either need explicitly set subject @answer or explicitly test @answer.

describe answer   { should validate_uniqueness_of(:correct).scoped_to(:question_id).with_message("correct can have 1 correct answer per question (true)") }   { should validate_uniqueness_of(:user_id).scoped_to(:question_id).with_message("user_id 1 answer per question per user (1)") } end 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -