Skip to content

Instantly share code, notes, and snippets.

@jferris
Created August 3, 2009 21:06
Show Gist options
  • Save jferris/160827 to your computer and use it in GitHub Desktop.
Save jferris/160827 to your computer and use it in GitHub Desktop.
class UsersControllerTest
# With context
context "on GET show, given the user exists" do
setup do
@user = Factory.stub(:user)
User.stubs(:find => @user)
get :show, :id => @user.to_param
end
should_respond_with :success
should_render_template :show
should "find the user" do
assert_received(User, :find) {|expect| expect.with(@user.to_param) }
end
should "display the user's email" do
assert_contain @user.email
end
end
# Flat
should "display the user on GET show for an existing user" do
user = Factory.stub(:user)
User.stubs(:find => user)
get :show, :id => user.to_param
assert_received(User, :find) {|expect| expect.with(user.to_param) }
assert_response :success
assert_template :show
assert_contain user.email
end
end
# One context per action
describe UsersController, "on GET show, given the user exists" do
before do
@user = Factory.stub(:user)
User.stubs(:find => @user)
get :show, :id => @user.to_param
end
it { should respond_with(:success) }
it { should render_template(:show) }
it "should find the user" do
User.should have_received(:find).with(@user.to_param)
end
end
# One test method per action
describe UsersController do
it "should display the user on GET show for an existing user" do
user = Factory.stub(:user)
User.stubs(:find => user)
get :show, :id => user.to_param
User.should have_received(:find).with(user.to_param)
should respond_with(:success)
should render_template(:show)
end
end
# Independent view test
describe "/users/show" do
before do
assigns[:user] = Factory.stub(:user)
render "/users/show"
end
it "should display the user's email" do
template.should contain(assigns[:user].email)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment