所以我有一个用户和评论模型。我正在为评论实施评级系统,以便我们可以跟踪谁已经对特定评论进行了投票。我想创建另一个类 (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
工厂.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*
宝石文件
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