mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Cleanup Action Dispatch assertions tests
Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
committed by
José Valim
parent
9532d746a9
commit
022fd876bb
@@ -2,34 +2,26 @@ require 'abstract_unit'
|
||||
require 'action_controller/vendor/html-scanner'
|
||||
require 'controller/fake_controllers'
|
||||
|
||||
# a controller class to facilitate the tests
|
||||
class ActionPackAssertionsController < ActionController::Base
|
||||
|
||||
# this does absolutely nothing
|
||||
def nothing() head :ok end
|
||||
|
||||
# a standard template
|
||||
def hello_world() render :template => "test/hello_world"; end
|
||||
|
||||
# a standard template
|
||||
def hello_xml_world() render :template => "test/hello_xml_world"; end
|
||||
|
||||
# a standard template rendering PDF
|
||||
def hello_xml_world_pdf
|
||||
self.content_type = "application/pdf"
|
||||
render :template => "test/hello_xml_world"
|
||||
end
|
||||
|
||||
# a standard template rendering PDF
|
||||
def hello_xml_world_pdf_header
|
||||
response.headers["Content-Type"] = "application/pdf; charset=utf-8"
|
||||
render :template => "test/hello_xml_world"
|
||||
end
|
||||
|
||||
# a standard partial
|
||||
def partial() render :partial => 'test/partial'; end
|
||||
|
||||
# a redirect to an internal location
|
||||
def redirect_internal() redirect_to "/nothing"; end
|
||||
|
||||
def redirect_to_action() redirect_to :action => "flash_me", :id => 1, :params => { "panda" => "fun" }; end
|
||||
@@ -42,31 +34,24 @@ class ActionPackAssertionsController < ActionController::Base
|
||||
|
||||
def redirect_to_named_route() redirect_to route_one_url end
|
||||
|
||||
# a redirect to an external location
|
||||
def redirect_external() redirect_to "http://www.rubyonrails.org"; end
|
||||
|
||||
# a 404
|
||||
def response404() head '404 AWOL' end
|
||||
|
||||
# a 500
|
||||
def response500() head '500 Sorry' end
|
||||
|
||||
# a fictional 599
|
||||
def response599() head '599 Whoah!' end
|
||||
|
||||
# putting stuff in the flash
|
||||
def flash_me
|
||||
flash['hello'] = 'my name is inigo montoya...'
|
||||
render :text => "Inconceivable!"
|
||||
end
|
||||
|
||||
# we have a flash, but nothing is in it
|
||||
def flash_me_naked
|
||||
flash.clear
|
||||
render :text => "wow!"
|
||||
end
|
||||
|
||||
# assign some template instance variables
|
||||
def assign_this
|
||||
@howdy = "ho"
|
||||
render :inline => "Mr. Henke"
|
||||
@@ -84,61 +69,20 @@ class ActionPackAssertionsController < ActionController::Base
|
||||
render :text => "Hello!", :content_type => Mime::RSS
|
||||
end
|
||||
|
||||
# puts something in the session
|
||||
def session_stuffing
|
||||
session['xmas'] = 'turkey'
|
||||
render :text => "ho ho ho"
|
||||
end
|
||||
|
||||
# raises exception on get requests
|
||||
def raise_on_get
|
||||
def raise_exception_on_get
|
||||
raise "get" if request.get?
|
||||
render :text => "request method: #{request.env['REQUEST_METHOD']}"
|
||||
end
|
||||
|
||||
# raises exception on post requests
|
||||
def raise_on_post
|
||||
def raise_exception_on_post
|
||||
raise "post" if request.post?
|
||||
render :text => "request method: #{request.env['REQUEST_METHOD']}"
|
||||
end
|
||||
|
||||
def get_valid_record
|
||||
@record = Class.new do
|
||||
def valid?
|
||||
true
|
||||
end
|
||||
|
||||
def errors
|
||||
Class.new do
|
||||
def full_messages; []; end
|
||||
end.new
|
||||
end
|
||||
|
||||
end.new
|
||||
|
||||
render :nothing => true
|
||||
end
|
||||
|
||||
|
||||
def get_invalid_record
|
||||
@record = Class.new do
|
||||
|
||||
def valid?
|
||||
false
|
||||
end
|
||||
|
||||
def errors
|
||||
Class.new do
|
||||
def full_messages; ['...stuff...']; end
|
||||
end.new
|
||||
end
|
||||
end.new
|
||||
|
||||
render :nothing => true
|
||||
end
|
||||
|
||||
# 911
|
||||
def rescue_action(e) raise; end
|
||||
end
|
||||
|
||||
# Used to test that assert_response includes the exception message
|
||||
@@ -181,46 +125,37 @@ module Admin
|
||||
end
|
||||
end
|
||||
|
||||
# require "action_dispatch/test_process"
|
||||
|
||||
# a test case to exercise the new capabilities TestRequest & TestResponse
|
||||
class ActionPackAssertionsControllerTest < ActionController::TestCase
|
||||
# -- assertion-based testing ------------------------------------------------
|
||||
|
||||
def test_assert_tag_and_url_for
|
||||
get :render_url
|
||||
assert_tag :content => "/action_pack_assertions/flash_me"
|
||||
end
|
||||
|
||||
# test the get method, make sure the request really was a get
|
||||
def test_get
|
||||
assert_raise(RuntimeError) { get :raise_on_get }
|
||||
get :raise_on_post
|
||||
def test_get_request
|
||||
assert_raise(RuntimeError) { get :raise_exception_on_get }
|
||||
get :raise_exception_on_post
|
||||
assert_equal @response.body, 'request method: GET'
|
||||
end
|
||||
|
||||
# test the get method, make sure the request really was a get
|
||||
def test_post
|
||||
assert_raise(RuntimeError) { post :raise_on_post }
|
||||
post :raise_on_get
|
||||
def test_post_request
|
||||
assert_raise(RuntimeError) { post :raise_exception_on_post }
|
||||
post :raise_exception_on_get
|
||||
assert_equal @response.body, 'request method: POST'
|
||||
end
|
||||
|
||||
# the following test fails because the request_method is now cached on the request instance
|
||||
# test the get/post switch within one test action
|
||||
# def test_get_post_switch
|
||||
# post :raise_on_get
|
||||
# assert_equal @response.body, 'request method: POST'
|
||||
# get :raise_on_post
|
||||
# assert_equal @response.body, 'request method: GET'
|
||||
# post :raise_on_get
|
||||
# assert_equal @response.body, 'request method: POST'
|
||||
# get :raise_on_post
|
||||
# assert_equal @response.body, 'request method: GET'
|
||||
# end
|
||||
def test_get_post_request_switch
|
||||
post :raise_exception_on_get
|
||||
assert_equal @response.body, 'request method: POST'
|
||||
get :raise_exception_on_post
|
||||
assert_equal @response.body, 'request method: GET'
|
||||
post :raise_exception_on_get
|
||||
assert_equal @response.body, 'request method: POST'
|
||||
get :raise_exception_on_post
|
||||
assert_equal @response.body, 'request method: GET'
|
||||
end
|
||||
|
||||
# test the redirection to a named route
|
||||
def test_assert_redirect_to_named_route
|
||||
def test_redirect_to_named_route
|
||||
with_routing do |set|
|
||||
set.draw do
|
||||
match 'route_one', :to => 'action_pack_assertions#nothing', :as => :route_one
|
||||
@@ -297,42 +232,44 @@ class ActionPackAssertionsControllerTest < ActionController::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
# -- standard request/response object testing --------------------------------
|
||||
|
||||
# make sure that the template objects exist
|
||||
def test_template_objects_alive
|
||||
def test_template_objects_exist
|
||||
process :assign_this
|
||||
assert !@controller.instance_variable_get(:"@hi")
|
||||
assert @controller.instance_variable_get(:"@howdy")
|
||||
end
|
||||
|
||||
# make sure we don't have template objects when we shouldn't
|
||||
def test_template_object_missing
|
||||
def test_template_objects_missing
|
||||
process :nothing
|
||||
assert_nil @controller.instance_variable_get(:@howdy)
|
||||
end
|
||||
|
||||
# check the empty flashing
|
||||
def test_flash_me_naked
|
||||
def test_empty_flash
|
||||
process :flash_me_naked
|
||||
assert flash.empty?
|
||||
end
|
||||
|
||||
# check if we have flash objects
|
||||
def test_flash_haves
|
||||
def test_flash_exist
|
||||
process :flash_me
|
||||
assert flash.any?
|
||||
assert_present flash['hello']
|
||||
end
|
||||
|
||||
# ensure we don't have flash objects
|
||||
def test_flash_have_nots
|
||||
def test_flash_does_not_exist
|
||||
process :nothing
|
||||
assert flash.empty?
|
||||
end
|
||||
|
||||
# check if we were rendered by a file-based template?
|
||||
def test_rendered_action
|
||||
def test_session_exist
|
||||
process :session_stuffing
|
||||
assert_equal session['xmas'], 'turkey'
|
||||
end
|
||||
|
||||
def session_does_not_exist
|
||||
process :nothing
|
||||
assert session.empty?
|
||||
end
|
||||
|
||||
def test_render_template_action
|
||||
process :nothing
|
||||
assert_template nil
|
||||
|
||||
@@ -340,7 +277,6 @@ class ActionPackAssertionsControllerTest < ActionController::TestCase
|
||||
assert_template 'hello_world'
|
||||
end
|
||||
|
||||
# check the redirection location
|
||||
def test_redirection_location
|
||||
process :redirect_internal
|
||||
assert_equal 'http://test.host/nothing', @response.redirect_url
|
||||
@@ -354,7 +290,6 @@ class ActionPackAssertionsControllerTest < ActionController::TestCase
|
||||
assert_nil @response.redirect_url
|
||||
end
|
||||
|
||||
# check server errors
|
||||
def test_server_error_response_code
|
||||
process :response500
|
||||
assert @response.server_error?
|
||||
@@ -366,19 +301,16 @@ class ActionPackAssertionsControllerTest < ActionController::TestCase
|
||||
assert !@response.server_error?
|
||||
end
|
||||
|
||||
# check a 404 response code
|
||||
def test_missing_response_code
|
||||
process :response404
|
||||
assert @response.missing?
|
||||
end
|
||||
|
||||
# check client errors
|
||||
def test_client_error_response_code
|
||||
process :response404
|
||||
assert @response.client_error?
|
||||
end
|
||||
|
||||
# check to see if our redirection matches a pattern
|
||||
def test_redirect_url_match
|
||||
process :redirect_external
|
||||
assert @response.redirect?
|
||||
@@ -386,7 +318,6 @@ class ActionPackAssertionsControllerTest < ActionController::TestCase
|
||||
assert !/perloffrails/.match(@response.redirect_url)
|
||||
end
|
||||
|
||||
# check for a redirection
|
||||
def test_redirection
|
||||
process :redirect_internal
|
||||
assert @response.redirect?
|
||||
@@ -398,14 +329,12 @@ class ActionPackAssertionsControllerTest < ActionController::TestCase
|
||||
assert !@response.redirect?
|
||||
end
|
||||
|
||||
# check a successful response code
|
||||
def test_successful_response_code
|
||||
process :nothing
|
||||
assert @response.success?
|
||||
end
|
||||
|
||||
# a basic check to make sure we have a TestResponse object
|
||||
def test_has_response
|
||||
def test_response_object
|
||||
process :nothing
|
||||
assert_kind_of ActionController::TestResponse, @response
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user