Add tests for hydrants controller

This commit is contained in:
Erik Michaels-Ober 2011-05-15 23:32:46 -07:00
parent e57c985861
commit 391f5c1e55
3 changed files with 131 additions and 7 deletions

View File

@ -23,7 +23,7 @@ class HydrantsController < ApplicationController
unless @hydrants.blank?
respond_with @hydrants
else
render(:json => {"errors" => {"address" => ["Could not find address."]}})
render(:json => {"errors" => {"address" => ["Could not find hydrants."]}}, :status => 404)
end
end
@ -32,7 +32,7 @@ class HydrantsController < ApplicationController
if @hydrant.update_attributes(params[:hydrant])
respond_with @hydrant
else
render(:json => {"errors" => @hydrant.errors})
render(:json => {"errors" => @hydrant.errors}, :status => 500)
end
end
end

View File

@ -1,11 +1,8 @@
%h2
= @hydrant.name ? @hydrant.name.titleize : "This hydrant"
has been adopted by
= @hydrant.user.name
= "#{@hydrant.name ? @hydrant.name.titleize : 'This hydrant'} has been adopted by #{@hydrant.user.name}"
- unless @hydrant.user.organization.blank?
%h3
of
= @hydrant.user.organization
= "of #{@hydrant.user.organization}"
- if user_signed_in?
= render :partial => 'users/edit_profile'
= render :partial => 'hydrants/steal'

View File

@ -0,0 +1,127 @@
require 'test_helper'
class HydrantsControllerTest < ActionController::TestCase
include Devise::TestHelpers
setup do
@hydrant = hydrants(:hydrant_1)
@user = users(:erik)
end
test 'should thank the user if the user the hydrant is adopted by the user' do
sign_in @user
@hydrant.user_id = @user.id
@hydrant.save!
get :show, :hydrant_id => @hydrant.id
assert_not_nil assigns :hydrant
assert_response :success
assert_template 'users/thank_you'
assert_select 'h2', 'Thank you for adopting this hydrant!'
assert_select 'form#edit_profile_form' do
assert_select '[action=?]', '/users/edit'
assert_select '[method=?]', 'get'
end
assert_select 'input[name="commit"]' do
assert_select '[type=?]', 'submit'
assert_select '[value=?]', 'Edit profile'
end
assert_select 'form#abandon_form' do
assert_select '[action=?]', "/hydrant.#{@hydrant.id}"
assert_select '[method=?]', 'post'
end
assert_select 'input[name="_method"]' do
assert_select '[type=?]', 'hidden'
assert_select '[value=?]', 'put'
end
assert_select 'input[name="commit"]' do
assert_select '[type=?]', 'submit'
assert_select '[value=?]', 'Abandon this hydrant'
end
assert_select 'form#sign_out_form' do
assert_select '[action=?]', '/hydrant'
assert_select '[method=?]', 'post'
end
assert_select 'input[name="commit"]' do
assert_select '[type=?]', 'submit'
assert_select '[value=?]', 'Sign out'
end
end
test 'should show the profile if the hydrant is adopted' do
@hydrant.user_id = @user.id
@hydrant.save!
get :show, :hydrant_id => @hydrant.id
assert_not_nil assigns :hydrant
assert_response :success
assert_template 'users/profile'
assert_select 'h2', "This hydrant has been adopted by #{@user.name}"
assert_select 'h3', "of #{@user.organization}"
end
test 'should show adoption form if hydrant is not adopted' do
sign_in @user
get :show, :hydrant_id => @hydrant.id
assert_not_nil assigns :hydrant
assert_response :success
assert_template :adopt
assert_select 'h2', 'Adopt this Hydrant'
assert_select 'form#adoption_form' do
assert_select '[action=?]', "/hydrant.#{@hydrant.id}"
assert_select '[method=?]', 'post'
end
assert_select 'input[name="_method"]' do
assert_select '[type=?]', 'hidden'
assert_select '[value=?]', 'put'
end
assert_select 'input[name="commit"]' do
assert_select '[type=?]', 'submit'
assert_select '[value=?]', 'Adopt!'
end
assert_select 'form#edit_profile_form' do
assert_select '[action=?]', '/users/edit'
assert_select '[method=?]', 'get'
end
assert_select 'input[name="commit"]' do
assert_select '[type=?]', 'submit'
assert_select '[value=?]', 'Edit profile'
end
assert_select 'form#sign_out_form' do
assert_select '[action=?]', '/hydrant'
assert_select '[method=?]', 'post'
end
assert_select 'input[name="commit"]' do
assert_select '[type=?]', 'submit'
assert_select '[value=?]', 'Sign out'
end
end
test 'should show sign-in form if signed out' do
get :show, :hydrant_id => @hydrant.id
assert_not_nil assigns :hydrant
assert_response :success
assert_template 'sessions/new'
assert_select 'form#combo_form' do
assert_select '[action=?]', '/hydrant'
assert_select '[method=?]', 'post'
end
assert_select 'h2', 'Adopt this Hydrant'
assert_select 'input', :count => 15
assert_select 'label', :count => 10
assert_select 'input[name="commit"]', :count => 3
end
# test 'should list hydrants' do
# skip 'Cannot test query on sqlite3 test database'
# get :list, :format => 'json', :lat => 42.358431, :lng => -71.059773
# assert_not_nil assigns :hydrants
# assert_response :success
# end
test 'should update hydrant' do
assert_not_equal 'Birdsill', @hydrant.name
put :update, :format => 'json', :id => @hydrant.id, :hydrant => {:name => 'Birdsill'}
@hydrant.reload
assert_equal 'Birdsill', @hydrant.name
assert_not_nil assigns :hydrant
assert_response :success
end
end