| Class | Test::Rails::ControllerTestCase |
| In: |
lib/test/rails/controller_test_case.rb
|
| Parent: | Test::Rails::FunctionalTestCase |
ControllerTestCase allows controllers to be tested independent of their views.
The test class must be named after your controller class name, so if you‘re testing actions for the RouteController you would name your test case RouteControllerTest.
The test names should be in the form of test_action_edgecase where ‘action’ corresponds to the name of the controller action, and ‘edgecase’ describes the scenario you are testing.
If you are testing an action named ‘show’ your test should be named test_show. If your action behaves differently depending upon its arguments then you can make the test name descriptive like test_show_photos and test_show_no_photos.
class RouteControllerTest < Test::Rails::ControllerTestCase
fixtures :users, :routes, :points, :photos
def test_delete
# Store current count
count = Route.count
# Set up our environment
session[:username] = users(:herbert).username
# perform the delete action
get :delete, :id => routes(:work).id
# Assert we got a 200
assert_response :success
# Assert controller deleted route
assert_equal count-1, Route.count
# Ensure that @action_title is set properly
assert_assigned :action_title, "Deleting \"#{routes(:work).name}\""
# Ensure that @route is set properly
assert_assigned :route, routes(:work)
end
end
class ApplicationController < ActionController::Base
##
# Send an email when we get an unhandled exception.
def log_error(exception)
case exception
when ::ActionController::RoutingError,
::ActionController::UnknownAction,
::ActiveRecord::RecordNotFound then
# ignore
else
unless RAILS_ENV == 'development' then
Email.deliver_error exception, params, session, @request.env
end
end
end
end
##
# Dummy Controller just for testing.
class DummyController < ApplicationController
def error
# Simulate a bug in our application
raise RuntimeError
end
end
class DummyControllerTest < Test::Rails::ControllerTestCase
def test_error_email
# The rescue_action added by ControllerTestCase needs to be removed so
# that exceptions fall through to the real error handler
@controller.class.send :remove_method, :rescue_action
# Fire off a request
get :error
# We should get a 500
assert_response 500
# And one delivered email
assert_equal 1, @deliveries.length, 'error email sent'
end
end
| NOTHING | = | Object.new # :nodoc: |
| DEFAULT_ASSIGNS | = | %w[ _cookies _flash _headers _params _request _response _session cookies flash headers params request response session action_name before_filter_chain_aborted db_rt_after_render db_rt_before_render ignore_missing_templates loggedin_user logger rendering_runtime request_origin template template_class template_root url user variables_added ] |
Asserts that the assigns variable ivar is assigned to value. If value is omitted, asserts that assigns variable ivar exists.
Asserts that key of flash has content. If content is a Regexp, then the assertion will fail if the Regexp does not match.
controller:
flash[:notice] = 'Please log in'
test:
assert_flash :notice, 'Please log in'
Checks your assert_assigned tests against the instance variables in assigns. Fails if the two don‘t match.
Add util_audit_assert_assigned to your teardown. If you have instance variables that you don‘t need to set (for example, were set in a before_filter in ApplicationController) then add them to the @assigns_ignored instance variable in your setup.
class UserController < ApplicationController
def new
# ...
@login_form = false
end
end
class UserControllerTest < Test::Rails::ControllerTestCase
def teardown
super
util_audit_assert_assigned
end
def test_new
get :new
assert_response :success
# no assert_assigns for @login_form
end
end
1) Failure:
test_new(UserControllerTest)
[[...]/controller_test_case.rb:331:in `util_audit_assert_assigned'
[...]/user_controller_test.rb:14:in `teardown_without_fixtures'
[...]fixtures.rb:555:in `teardown']:
You are missing these assert_assigned assertions:
assert_assigned :login_form #, some_value
.