I want to write a test using Rspec and Capybara.
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.
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:
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?)
I've been following a basic pattern of model tests and integration tests. I've also been using FactoryGirl along with rspec and capybara. So... first off here is what my factory might look like:
Then I would make a model spec file (spec/models/post_spec.rb):
Then I would make an integration test (spec/requests/posts_spec.rb):
You might try working through the rails tutorial at http://ruby.railstutorial.org/ It uses rspec, capybara and FactoryGirl for the tests.
To add on to new2ruby's answer you can also use the Feature/Scenario aliases provided by capybara in your integration tests.
I find it reads nicely:
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.