Ruby on rails 测试此帖子和评论模型之间的关联?
在前面的问题中,我问了如何为Ruby on rails 测试此帖子和评论模型之间的关联?,ruby-on-rails,rspec,Ruby On Rails,Rspec,在前面的问题中,我问了如何为Post和用户model构建测试。现在我想测试第三个模型,名为Comment schema.rb: create_table "posts", :force => true do |t| t.string "title" t.string "content" t.integer "user_id" t.datetime "created_at", :null
Post
和用户model
构建测试。现在我想测试第三个模型,名为Comment
schema.rb:
create_table "posts", :force => true do |t|
t.string "title"
t.string "content"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "comments_count", :default => 0, :null => false
t.datetime "published_at"
t.boolean "draft", :default => false
end
create_table "comments", :force => true do |t|
t.text "content"
t.integer "post_id"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
class Comment < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :post, :counter_cache => true
belongs_to :user
end
FactoryGirl.define do
factory :user do
username "Michael Hartl"
email "michael@example.com"
password "foobar"
password_confirmation "foobar"
end
end
FactoryGirl.define do
factory :post do
title "Sample Title"
content "Sample Content"
published_at Time.now()
comments_count 0
draft false
association :user
end
end
require 'spec_helper'
describe Post do
let(:post) { FactoryGirl.create(:post) }
subject { post }
it { should respond_to(:title) }
it { should respond_to(:content) }
it { should respond_to(:user_id) }
it { should respond_to(:user) }
it { should respond_to(:published_at) }
it { should respond_to(:draft) }
it { should respond_to(:comments_count) }
its(:draft) { should == false }
it { should be_valid }
end
我特别想测试评论数
:我想在帖子中创建评论。他们之间已经有了联系(post有很多评论)。并检查注释计数是否增加
有谁能给我举个测试的例子吗
当前代码:
comment.rb:
create_table "posts", :force => true do |t|
t.string "title"
t.string "content"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "comments_count", :default => 0, :null => false
t.datetime "published_at"
t.boolean "draft", :default => false
end
create_table "comments", :force => true do |t|
t.text "content"
t.integer "post_id"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
class Comment < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :post, :counter_cache => true
belongs_to :user
end
FactoryGirl.define do
factory :user do
username "Michael Hartl"
email "michael@example.com"
password "foobar"
password_confirmation "foobar"
end
end
FactoryGirl.define do
factory :post do
title "Sample Title"
content "Sample Content"
published_at Time.now()
comments_count 0
draft false
association :user
end
end
require 'spec_helper'
describe Post do
let(:post) { FactoryGirl.create(:post) }
subject { post }
it { should respond_to(:title) }
it { should respond_to(:content) }
it { should respond_to(:user_id) }
it { should respond_to(:user) }
it { should respond_to(:published_at) }
it { should respond_to(:draft) }
it { should respond_to(:comments_count) }
its(:draft) { should == false }
it { should be_valid }
end
规格/型号/后规格rb:
create_table "posts", :force => true do |t|
t.string "title"
t.string "content"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "comments_count", :default => 0, :null => false
t.datetime "published_at"
t.boolean "draft", :default => false
end
create_table "comments", :force => true do |t|
t.text "content"
t.integer "post_id"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
class Comment < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :post, :counter_cache => true
belongs_to :user
end
FactoryGirl.define do
factory :user do
username "Michael Hartl"
email "michael@example.com"
password "foobar"
password_confirmation "foobar"
end
end
FactoryGirl.define do
factory :post do
title "Sample Title"
content "Sample Content"
published_at Time.now()
comments_count 0
draft false
association :user
end
end
require 'spec_helper'
describe Post do
let(:post) { FactoryGirl.create(:post) }
subject { post }
it { should respond_to(:title) }
it { should respond_to(:content) }
it { should respond_to(:user_id) }
it { should respond_to(:user) }
it { should respond_to(:published_at) }
it { should respond_to(:draft) }
it { should respond_to(:comments_count) }
its(:draft) { should == false }
it { should be_valid }
end
(顺便说一句,这是我第一次在我的应用程序中测试一些东西。我测试的是不需要测试的东西吗?是否缺少应该测试的东西?我们可能需要一个工厂来提供意见:
FactoryGirl.define do
factory :comment do
content "Sample comemnt"
association :user
association :post
end
end
下面的测试(我在请求测试中加入了该测试)将进行检查,以确保在用户向表单添加内容并单击右按钮时,确实添加了注释:
describe "New comments" do
let(:post) FactoryGirl.create(:post)
let(:user) FactoryGirl.create(:user)
context "valid with content comment added to database" do
before do
visit post_path(post)
fill_in 'Content', with: "A new comment."
end
expect { click_button 'Create Comment' }.to change(Comment, :count).by(1)
end
end
此测试可能适用于注释模型规范:
describe Comment do
let(:comment) { FactoryGirl.create(:comment) }
subject { comment }
it { should respond_to(:content) }
it { should respond_to(:user_id) }
it { should respond_to(:user) }
it { should respond_to(:post_id) }
it { should be_valid }
it "should belong to a post which has a comment count of 1" do
comment.post.comment_count.should equal 1
end
end
然后,让这个测试通过的方法是在评论模型中放入一些东西,以便在创建新评论时,更新它所属的帖子中的comment\u count属性
我不能100%确定最后一次测试写得是否正确。我不确定您是否可以覆盖先前定义的主题。我很好奇:comment\u count。您是否有不使用内置查询@post.comments.count的原因?@new2ruby:counter\u cache=>true
提高性能。计数是缓存的,当发生更改时,只有一个查询。此时性能是一个巨大的考虑因素吗?一直在帮助我学习rails的那个家伙一直在唠叨我,说我不应该花时间优化那些可能永远不会成为问题的东西。即使我觉得他这样做很烦人,我也不得不用同样的方式来烦扰你@凯文哈,好吧,如果我没有这么做,我以后可能会忘记这么做。非常感谢!我会试试这个。