【问题标题】:Testing the association between this Post and Comment model?测试这个帖子和评论模型之间的关联?
【发布时间】:2012-10-25 03:44:10
【问题描述】:

在this 上一个问题中,我询问了如何为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

我特别想测试comments_count:一个想在帖子中创建cmets。他们的关联已经建立(发布has_many cmets)。并检查comments_count 是否增加。

谁能给我一个测试的例子吗?

当前代码:

comment.rb:

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

spec/models/post_spec.rb:

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

(顺便说一句,这是我第一次在我的应用中测试一些东西。我是在测试一些不需要测试的东西吗?是否缺少一些应该测试的东西?)

【问题讨论】:

  • 我对 :comment_count 很好奇。您是否有理由不使用内置查询 @post.cmets.count?
  • @new2ruby :counter_cache =&gt; true 提高性能。计数被缓存,只有一个查询何时发生变化。
  • 在这一点上性能是一个重要的考虑因素吗?一直在帮助我使用 Rails 的那个人一直对我说,我不应该花时间优化可能永远不会成为问题的东西。尽管我觉得他这样做很烦人,但我还是不得不以同样的方式惹恼你。 :)
  • @KevinM Ha,好吧,如果我没有这样做,我可能会忘记将来这样做。

标签: ruby-on-rails rspec


【解决方案1】:

我们可能需要一个 cmets 工厂:

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_count属性。

我不能 100% 确定最后一个测试是否正确编写。我不确定你是否可以覆盖之前定义的主题。

【讨论】:

  • 非常感谢!我会试试这个。
猜你喜欢
  • 2018-08-17
  • 2022-11-09
  • 1970-01-01
  • 2022-09-30
  • 1970-01-01
  • 1970-01-01
  • 2020-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多