通过关联暗示的评论对象是零?

时间:2013-09-21 01:10:02

标签: ruby-on-rails rspec null

所以我有一个用户和评论模型。我正在为评论实施评级系统,以便我们可以跟踪已经对某个评论投票的人。我想创建另一个类(who_rated_comment_rel)来描述用户评价评论时创建的关系。我正在进行测试驱动开发,所以我首先编写测试。但是我有这个问题,我现在已经工作了几个小时了,但我似乎无法超越它。以下是相关代码,最重要的部分是强调。

rspec spec/model 
gives me the following error:

**Failures:**

 1) WhoRatedCommentRel follower methods comment 
 Failure/Error: its(:comment) { should eq comment }

   expected: #<Comment id: 4, created_at: "2013-09-21 00:28:44", updated_at: "2013-09-21 00:28:44", usr_id: 4, upd_id: 4, com_id: 4, content: "Lorem Ipsum", rating: nil, who_rated: nil>
        got: nil

   (compared using ==)
 # ./spec/models/who_rated_comment_rel_spec.rb:21:in `block (3 levels) in <top (required)>'

...

Failed examples:

rspec ./spec/models/who_rated_comment_rel_spec.rb:21 # WhoRatedCommentRel follower methods comment 

factories.rb

FactoryGirl.define do 
    factory :user do
        sequence(:name)  { |n| "Person #{n}" }
        sequence(:email) { |n| "person_#{n}@example.com"}
    password "foobar123"
        password_confirmation "foobar123"

        factory :admin do
        admin true
        end
    end

    factory :comment do
        sequence(:usr_id) { |n| n }
        sequence(:upd_id) { |n| n }
        sequence(:com_id) { |n| n }

        content "Lorem Ipsum"

        factory :usr_1 do
        usr_id 1
        end

    factory :usr_2 do
        usr_id 2
    end

    factory :upd_1 do
        upd_id 1
    end

    factory :upd_2 do
        upd_id 2
    end


    end

    factory :pin do 
        description "Build the next Facebook"
        user
    end
end

规格/模型/ who_rated_comment_rel_spec.rb:

require 'spec_helper'


describe WhoRatedCommentRel do
    let(:user)    { FactoryGirl.create(:user) }
    let(:comment) { FactoryGirl.create(:comment) }
    let(:voting_rel) do
    user.who_rated_comment_rels.build(comment_id: comment.id) 
end

subject { voting_rel }

it { should be_valid }


describe "follower methods" do

    it { should respond_to(:user_id) }
    it { should respond_to(:comment_id) }
    its(:comment) { should eq comment }
    its(:user)    { should eq user }
  end
end

应用/模型/ who_rated_comment_rel.rb

class WhoRatedCommentRel < ActiveRecord::Base
    *belongs_to :user,    class_name: "User"*
    *belongs_to :comment, class_name: "Comment"* 
end

应用/模型/ comment.rb:

class Comment < ActiveRecord::Base
    validates :usr_id, presence: true
    validates :content, presence: true
    validates :upd_id, presence: true

    attr_accessible :usr_id, :content, :upd_id

    def upvote 

    end

    def downvote

    end
end

class User < ActiveRecord::Base
    before_save { self.email = email.downcase }

    devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable

    attr_accessible :name, :email, :password, :password_confirmation, :remember_me

    validates :name, presence: true, 
                 uniqueness: { case_sensitive: false },
                 length: { maximum: 30 }
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
     validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                                    uniqueness: { case_sensitive: false }

    has_many :pins, dependent: :destroy
    *has_many :who_rated_comment_rels, foreign_key: "user_id", dependent: :destroy*

的Gemfile

source 'https://rubygems.org'

ruby '2.0.0'

gem 'rails', '4.0.0'
gem 'bootstrap-sass', '~> 2.3.2.1'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
gem 'haml'

gem 'simple_form', git: 'git://github.com/plataformatec/simple_form.git'
gem 'devise'
gem 'protected_attributes'

gem 'sass-rails',   '4.0.0'
gem 'uglifier',         '2.1.1'
gem 'coffee-rails', '4.0.0'
gem 'jquery-rails'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'

group :development, :test do 
    gem 'sqlite3', '1.3.7'
    gem 'rspec-rails', '2.13.1' # for testing.
    gem 'debugger'
end

group :test do
    gem 'selenium-webdriver', '2.35.1'
    gem 'capybara', '2.1.0'
    gem 'factory_girl_rails', '4.2.0'
end

group :doc do
    gem 'sdoc', '0.3.20', require: false
end 

group :production do 
    gem 'pg', '0.15.1'
    gem 'rails_12factor', '0.0.2'
end

1 个答案:

答案 0 :(得分:0)

我想通了,乡亲们。

问题是我仍在使用* protected_attributes * gem,并且将comment.id传递给关系的新实例不允许我因为我没有使用rated_comment_id * attr_accessible *。不幸的是,RSpec没有抱怨群发任务问题。我只是在将相同的命令输入rails控制台(沙盒)时才弄明白。