Created
August 26, 2009 07:41
-
-
Save benvds/175370 to your computer and use it in GitHub Desktop.
Controller test for default resource using Shoulda and Factory_Girl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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