Added associations and tested them

This commit is contained in:
gnattery
2013-02-06 15:20:44 +11:00
parent 3cff543184
commit 6da3e4fae5
7 changed files with 34 additions and 5 deletions

View File

@@ -1,3 +1,5 @@
class Comment < ActiveRecord::Base
attr_accessible :author_id, :body, :post_id
belongs_to :author, :class_name => 'Member'
belongs_to :post
end

View File

@@ -3,6 +3,7 @@ class Member < ActiveRecord::Base
friendly_id :login_name, use: :slugged
has_many :posts, :foreign_key => 'author_id'
has_many :comments, :foreign_key => 'author_id'
has_many :gardens, :foreign_key => 'owner_id'
# Include default devise modules. Others available are:

View File

@@ -3,6 +3,7 @@ class Post < ActiveRecord::Base
friendly_id :author_date_subject, use: :slugged
attr_accessible :body, :subject, :author_id
belongs_to :author, :class_name => 'Member'
has_many :comments
default_scope order("created_at desc")
def author_date_subject

View File

@@ -1,9 +1,7 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :comment do
post_id 1
author_id 1
body "MyText"
post
author
body "OMG LOL"
end
end

View File

@@ -1,2 +1,16 @@
require 'spec_helper'
describe Comment do
before(:each) do
@comment = FactoryGirl.create(:comment)
end
it "belongs to a post" do
@comment.post.should be_an_instance_of Post
end
it "belongs to an author" do
@comment.author.should be_an_instance_of Member
end
end

View File

@@ -44,6 +44,12 @@ describe 'member' do
@member.gardens.first.name.should eq "Garden"
end
it "has many comments" do
@member.save
@comment1 = FactoryGirl.create(:comment, :author => @member)
@comment2 = FactoryGirl.create(:comment, :author => @member)
@member.comments.length.should == 2
end
end
context 'no TOS agreement' do

View File

@@ -20,4 +20,11 @@ describe Post do
@datestr.length.should == 4 + @time.year.to_s.size
@post.slug.should == "#{@member.login_name}-#{@datestr}-a-post"
end
it "has many comments" do
@post = FactoryGirl.create(:post, :author => @member)
@comment1 = FactoryGirl.create(:comment, :post => @post)
@comment2 = FactoryGirl.create(:comment, :post => @post)
@post.comments.length.should == 2
end
end