したがって、ユーザーとコメントのモデルがあります。特定のコメントに誰がすでに投票したかを追跡できるように、コメントの評価システムを実装中です。ユーザーがコメントを評価したときに作成される関係を記述する別のクラス (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
factory.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