测试此帖子模型的草图属性(Rspec + Capybara) [英] Testing the draft attribute of this post model (Rspec + Capybara)
问题描述
我有一个帖子
模型,我想向其中添加草稿
属性。如果用户选中该字段,则该帖子将另存为草稿( draft = true
)。只有具有 draft = false
属性的帖子才会显示在帖子索引页面中。 draft = true
的帖子只会显示在创建该帖子的用户页面中。
I have a Post
model and I want to add a draft
attribute to it. If the user checks that field the post is saved as draft (draft = true
). Only posts that have he attribute draft = false
will show in the post index page. Posts with draft = true
will only show in the page of the user who created that post.
我已经我一生中从未做过 Rspec + Capybara 测试。所以我想知道是否有人可以告诉我如何开始或举一个例子。
预先感谢!
I've never made a Rspec + Capybara test in my life. So I was wondering if someone could tell me how to start or give me an example. Thanks in advance!
schema.rb:
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
(顺便说一句,我是否需要发布模型,控制器或查看页面?)
(By the way, do I need to post the model, controller or view pages?)
推荐答案
要添加到new2ruby的答案中,您还可以在集成中使用capybara提供的功能/场景别名测试。
To add on to new2ruby's answer you can also use the Feature/Scenario aliases provided by capybara in your integration tests.
我觉得它很好看:
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
我最近写了一篇有关使用 RSpec与Capybara进行集成测试。让我知道您是否对设置代码库有任何疑问。
I recently wrote a "getting started" blog post on using RSpec with Capybara for integration tests. Let me know if you have any questions on getting your codebase set up.
这篇关于测试此帖子模型的草图属性(Rspec + Capybara)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!