ユーザーが記事の作成者である単純な記事とユーザーの関連付けをまとめる
class Article < ActiveRecord::Base
attr_accessor :id, :name, :title, :title_image, :keywords, :related_urls, :content, :meldd
validates_presence_of :title, :name, :title_image, :keywords, :related_urls, :content
as_enum :status, [:new, :draft, :private, :published], :column => "article_status", :prefix => true
validates_as_enum :status
belongs_to :author, :class_name => 'User'
end
class User < ActiveRecord::Base
validates_format_of :pen_name, :with => /\A[[:word]]+\Z/
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
has_many :articles, :foreign_key => 'author_id'
easy_roles :roles
end
関連するスペックは require 'spec_helper' require_relative '../support/cells/articles/recent_updates_data' です。
describe Article do
include ArticlesCellSpecHelpers
before(:each) do
@it = stub_model Article
end
describe "basic, existential structure" do
it "starts with blank attributes" do
@it.name.should be_nil
@it.title.should be_nil
@it.title_image.should be_nil
@it.keywords.should be_nil
@it.related_urls.should be_nil
@it.content.should be_nil
end
it "is an ActiveRecord subclass" do
@it.should be_a ActiveRecord::Base
end
describe "associations" do
it "has an author" do
author = stub_model User, :pen_name => 'Joe Blow', :email => 'joe.blow@example.com'
params = recent_updates_data.first()
params[:author_id] = author.id
params[:status] = :published
article = stub_model Article, params
article.author_id.should == author.id
end
end
非常に奇妙ですが、「belongs_to :author, :class_name => 'User'」をコメントアウトすると、すべてのテストに問題はありませんが、その行が含まれていると、次のエラーがスローされます。
Article basic, existential structure is an ActiveRecord subclass
Failure/Error: @it = stub_model Article
TypeError:
can't define singleton
# ./spec/models/article_spec.rb:9:in `block (2 levels) in <top (required)>'
これを修正する方法について何か提案はありますか? ありがとう!!