Exercise-3-2
## Rails specific assertions
## originally found here http://manuals.rubyonrails.com/read/chapter/28
## modified for examples

## Rails Controller Response Assertions

# assert_template ( expected_template, [msg] )
# ensures the expected template was responsible for rendering.

assert_template("user/profile") # asserts that app/views/user/profile.rhtml was rendered

# assert_response ( type_or_code, [msg] )
# ensures the response type/status code is as expected
#   :success (status code is 200)
#   :redirect (status code is within 300..399)
#   :missing (status code is 404)
#   :error (status code is within 500..599)
#   :any number (to specifically reference a particular status code)

assert_response :success      # page rendered ok
assert_response :redirect     # we've been redirected
assert_response :missing      # not found
assert_response 505           # status code was 505

# assert_redirected_to ( options={}, [msg] )
# ensures we’ve been redirected to a specific place within our application

assert_redirected_to :controller => 'widget', :action => 'view', :id => 555

## Rails Tag Assertions

# assert_tag ( options )
# ensures that a tag or text exists.
# assert_no_tag ( options )
# ensures that the tag does not exist.

assert_tag :tag => "span" # assert that there is a "span" tag
assert_tag :tag => "span", :parent => { :tag => "div" } # "span" inside a "div" 
assert_tag :tag => "span", :ancestor => { :tag => "table" } # "span" somewhere inside a table
assert_tag :tag => "span", :child => { :tag => "em" } # "span" with at least one "em" child

# assert that there is a "span" containing a (possibly nested) "strong" tag.
assert_tag :tag => "span", :descendant => { :tag => "strong" }

# assert that there is a "span" containing between 2 and 4 "em" tags
# as immediate children
assert_tag :tag => "span",
           :children => { :count => 2..4, :only => { :tag => "em" } }

## Rails Routing Assertions

# assert_generates ( expected_path, options, defaults={}, extras = {}, [msg] )
# ensures that the options map to the expected_path

opts = {:controller => "movies", :action => "movie", :id => "60"}
assert_generates "movies/movie/60", opts

# assert_recognizes ( expected_options, path, extras={}, [msg] )
# ensures that when the path is chopped up into pieces, it is equal to 
#   expected_options. Essentially, the opposite of assert_generates.

opts = {:controller => "movies", :action => "movie", :id => "60"}
assert_recognizes opts, "/movies/movie/60"

# assert_routing ( path, options, defaults={}, extras={}, [msg] )
# ensures that the path resolves into options, and the options, resolves into path
# If you’re going to test your routes, this assertion might be your best bet.

opts = {:controller => "movies", :action => "movie", :id => "60"}
assert_routing "movies/movie/60", opts

link download