Ruby on rails 测试此post模型的draft属性(Rspec&x2B;Capybara)

Ruby on rails 测试此post模型的draft属性(Rspec&x2B;Capybara),ruby-on-rails,rspec,capybara,Ruby On Rails,Rspec,Capybara,我想使用和编写一个测试 我有一个Post模型,我想给它添加一个draft属性。如果用户选中该字段,则文章将另存为草稿(draft=true)。只有具有属性draft=false的帖子才会显示在帖子索引页面中。带有draft=true的帖子将仅显示在创建该帖子的用户的页面中 我一生中从未做过Rspec+水豚试验。所以我想知道是否有人可以告诉我如何开始或给我一个例子。 提前谢谢 schema.rb: create_table "posts", :force => true do |t|

我想使用和编写一个测试

我有一个
Post
模型,我想给它添加一个
draft
属性。如果用户选中该字段,则文章将另存为草稿(
draft=true
)。只有具有属性
draft=false
的帖子才会显示在帖子索引页面中。带有
draft=true
的帖子将仅显示在创建该帖子的用户的页面中

我一生中从未做过Rspec+水豚试验。所以我想知道是否有人可以告诉我如何开始或给我一个例子。 提前谢谢

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

(顺便问一下,我需要发布模型、控制器或视图页面吗?

我一直遵循模型测试和集成测试的基本模式。我也一直在使用FactoryGirl以及rspec和水豚。所以首先,我的工厂可能是这样的:

FactoryGirl.define do
  factory :user do
    sequence(:email) { |n| "person#{n}@example.com" }
    password "foobar"
    password_confirmation "foobar"
  end

  factory :post do
    sequence(:title) { |n| "Test Title #{n}"}
    string "Test content."
    published_at Time.now()
    comments_count 0
    draft false
    association :user

    factory (:draft) do
      draft true
    end
  end
end
然后我将创建一个模型规范文件(spec/models/post_spec.rb):

然后我会做一个集成测试(spec/requests/posts\u spec.rb):


您可以尝试在rails教程中使用rspec、capybara和FactoryGirl进行测试

我一直在遵循模型测试和集成测试的基本模式。我也一直在使用FactoryGirl以及rspec和水豚。所以首先,我的工厂可能是这样的:

FactoryGirl.define do
  factory :user do
    sequence(:email) { |n| "person#{n}@example.com" }
    password "foobar"
    password_confirmation "foobar"
  end

  factory :post do
    sequence(:title) { |n| "Test Title #{n}"}
    string "Test content."
    published_at Time.now()
    comments_count 0
    draft false
    association :user

    factory (:draft) do
      draft true
    end
  end
end
然后我将创建一个模型规范文件(spec/models/post_spec.rb):

然后我会做一个集成测试(spec/requests/posts\u spec.rb):


您可以尝试在rails教程中使用rspec、capybara和FactoryGirl进行测试

要补充new2ruby的答案,您还可以在集成测试中使用capybara提供的功能/场景别名

我觉得它读起来很好:

require 'spec_helper'

feature 'Visitor views posts' do
    scenario 'shows completed posts' do 
        post = FactoryGirl.create(post)
        visit posts_path

        page.should have_content(post.title)
    end

    scenario 'does not show drafts' do
        draft = FactoryGirl.create(draft)
        visit posts_path

        page.should_not have_content(draft.title)
    end
end

我最近写了一篇关于使用的“入门”博客文章。如果您对设置代码库有任何疑问,请告诉我。

要补充new2ruby的答案,您还可以在集成测试中使用capybara提供的功能/场景别名

我觉得它读起来很好:

require 'spec_helper'

feature 'Visitor views posts' do
    scenario 'shows completed posts' do 
        post = FactoryGirl.create(post)
        visit posts_path

        page.should have_content(post.title)
    end

    scenario 'does not show drafts' do
        draft = FactoryGirl.create(draft)
        visit posts_path

        page.should_not have_content(draft.title)
    end
end

我最近写了一篇关于使用的“入门”博客文章。如果您对设置代码库有任何疑问,请告诉我。

非常感谢!我会试试看。顺便问一下,FactoryGirl文件应该放在哪里?非常感谢!我会试试看。顺便问一下,FactoryGirl文件应该放在哪里?很高兴知道。没有意识到如果这是你自己的内容,你必须大声说出来。我会确保我以后会这么做的。@harlow谢谢!(嘿,我很困惑。我以为“功能”是黄瓜的东西。)@alexchenco
feature/scenario
已添加到Capybara的别名
descripe/it
。这为RSpec用户带来了小黄瓜风格的集成,对历史上使用Cuc的人来说感觉更自然。@harlow,这意味着水豚正在取代黄瓜?@alexchenco不,一点也不。黄瓜仍然活得很好。然而,在内部,我们的团队已经开始选择RSpec+水豚而不是黄瓜。很高兴知道。没有意识到如果这是你自己的内容,你必须大声说出来。我会确保我以后会这么做的。@harlow谢谢!(嘿,我很困惑。我以为“功能”是黄瓜的东西。)@alexchenco
feature/scenario
已添加到Capybara的别名
descripe/it
。这为RSpec用户带来了小黄瓜风格的集成,对历史上使用Cuc的人来说感觉更自然。@harlow,这意味着水豚正在取代黄瓜?@alexchenco不,一点也不。黄瓜仍然活得很好。然而,在内部,我们的团队已经开始选择RSpec+水豚而不是黄瓜。