Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save benvds/175370 to your computer and use it in GitHub Desktop.
Save benvds/175370 to your computer and use it in GitHub Desktop.
Controller test for default resource using Shoulda and Factory_Girl
# posts_controller_test.rb
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
context "on GET to :index" do
setup do
@first_post = Factory(:post, :title => "My first post")
@second_post = Factory(:post, :title => "My second post")
get :index
end
should_respond_with :success
should_render_template :index
should_assign_to :posts
should_not_set_the_flash
end
context "on GET to :new" do
setup { get :new }
should_respond_with :success
should_render_template :new
should_not_set_the_flash
end
context "on POST to :create" do
setup { post :create, :post => {:title => 'My new post' } }
should_assign_to :post
should_redirect_to("the new post page") { post_path(assigns :post) }
should_set_the_flash_to(/created/i)
end
context "on GET to :show" do
setup do
@post = Factory(:post)
get :show, :id => @post.id
end
should_respond_with :success
should_render_template :show
should_assign_to :post
should_not_set_the_flash
end
context "on GET to :edit" do
setup do
@post = Factory(:post)
get :edit, :id => @post.id
end
should_respond_with :success
should_render_template :edit
should_assign_to :post
should_not_set_the_flash
end
context "on PUT to :update" do
setup do
@post = Factory(:post)
put :update, :id => @post.id, :post => {:name => 'asian' }
end
should_assign_to :post
should_redirect_to("the updated post page") { post_path(assigns :post) }
should_set_the_flash_to(/updated/i)
end
context "on DELETE to :destroy" do
setup do
@post = Factory(:post)
assert_difference('Post.count', -1) do
delete :destroy, :id => @post.id
end
end
should_assign_to :post
should_redirect_to("the posts page") { posts_path }
should_not_set_the_flash
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment