moduleShoulda# :nodoc:moduleActionController# :nodoc:# = Macro test helpers for your controllers## By using the macro helpers you can quickly and easily create concise and easy to read test suites.## This code segment:# context "on GET to :show for first record" do# setup do# get :show, :id => 1# end## should_assign_to :user# should_respond_with :success# should_render_template :show# should_not_set_the_flash## should "do something else really cool" do# assert_equal 1, assigns(:user).id# end# end## Would produce 5 tests for the +show+ actionmoduleMacrosincludeMatchers# Macro that creates a test asserting that the flash contains the given# value. Expects a +String+ or +Regexp+.## If the argument is +nil+, it will assert that the flash is not set.# This behavior is deprecated.## Example:## should_set_the_flash_to "Thank you for placing this order."# should_set_the_flash_to /created/idefshould_set_the_flash_to(val)ifvalmatcher=set_the_flash.to(val)shouldmatcher.descriptiondoassert_acceptsmatcher,@controllerendelsewarn"[DEPRECATION] should_set_the_flash_to nil is deprecated. "<<"Use should_not_set_the_flash instead."should_not_set_the_flashendend# Macro that creates a test asserting that the flash is empty.defshould_not_set_the_flashmatcher=set_the_flashshould"not #{matcher.description}"doassert_rejectsmatcher,@controllerendend# Macro that creates a test asserting that filter_parameter_logging# is set for the specified keys## Example:## should_filter_params :password, :ssndefshould_filter_params(*keys)keys.eachdo|key|matcher=filter_param(key)shouldmatcher.descriptiondoassert_acceptsmatcher,@controllerendendend# Macro that creates a test asserting that the controller assigned to# each of the named instance variable(s).## Options:# * <tt>:class</tt> - The expected class of the instance variable being checked.## If a block is passed, the assigned variable is expected to be equal to# the return value of that block.## Example:## should_assign_to :user, :posts# should_assign_to :user, :class => User# should_assign_to(:user) { @user }defshould_assign_to(*names,&block)klass=get_options!(names,:class)names.eachdo|name|matcher=assign_to(name).with_kind_of(klass)shouldmatcher.descriptiondoifblockexpected_value=instance_eval(&block)matcher=matcher.with(expected_value)endassert_acceptsmatcher,@controllerendendend# Macro that creates a test asserting that the controller did not assign to# any of the named instance variable(s).## Example:## should_not_assign_to :user, :postsdefshould_not_assign_to(*names)names.eachdo|name|matcher=assign_to(name)should"not #{matcher.description}"doassert_rejectsmatcher,@controllerendendend# Macro that creates a test asserting that the controller responded with a 'response' status code.# Example:## should_respond_with :successdefshould_respond_with(response)should"respond with #{response}"domatcher=respond_with(response)assert_acceptsmatcher,@controllerendend# Macro that creates a test asserting that the response content type was 'content_type'.# Example:## should_respond_with_content_type 'application/rss+xml'# should_respond_with_content_type :rss# should_respond_with_content_type /rss/defshould_respond_with_content_type(content_type)matcher=respond_with_content_type(content_type)shouldmatcher.descriptiondoassert_acceptsmatcher,@controllerendend# Macro that creates a test asserting that a value returned from the# session is correct. Expects the session key as a parameter, and a block# that returns the expected value.## Example:## should_set_session(:user_id) { @user.id }# should_set_session(:message) { "Free stuff" }defshould_set_session(key,&block)matcher=set_session(key)shouldmatcher.descriptiondoexpected_value=instance_eval(&block)matcher=matcher.to(expected_value)assert_acceptsmatcher,@controllerendend# Macro that creates a test asserting that the controller rendered the given template.# Example:## should_render_template :newdefshould_render_template(template)should"render template #{template.inspect}"doassert_templatetemplate.to_sendend# Macro that creates a test asserting that the controller rendered with the given layout.# Example:## should_render_with_layout 'special'defshould_render_with_layout(expected_layout='application')matcher=render_with_layout(expected_layout)ifexpected_layoutshouldmatcher.descriptiondoassert_acceptsmatcher,@controllerendelseshould"render without layout"doassert_rejectsmatcher,@controllerendendend# Macro that creates a test asserting that the controller rendered without a layout.# Same as @should_render_with_layout false@defshould_render_without_layoutshould_render_with_layoutnilend# Macro that creates a test asserting that the controller returned a# redirect to the given path. The passed description will be used when# generating a test name. Expects a block that returns the expected path# for the redirect.## Example:## should_redirect_to("the user's profile") { user_url(@user) }defshould_redirect_to(description,&block)should"redirect to #{description}"doexpected_url=instance_eval(&block)assert_redirected_toexpected_urlendend# Macro that creates a routing test. It tries to use the given HTTP# +method+ on the given +path+, and asserts that it routes to the# given +options+.## If you don't specify a :controller, it will try to guess the controller# based on the current test.## +to_param+ is called on the +options+ given.## Examples:## should_route :get, "/posts", :controller => :posts, :action => :index# should_route :get, "/posts/new", :action => :new# should_route :post, "/posts", :action => :create# should_route :get, "/posts/1", :action => :show, :id => 1# should_route :edit, "/posts/1", :action => :show, :id => 1# should_route :put, "/posts/1", :action => :update, :id => 1# should_route :delete, "/posts/1", :action => :destroy, :id => 1# should_route :get, "/users/1/posts/1",# :action => :show, :id => 1, :user_id => 1#defshould_route(method,path,options)unlessoptions[:controller]options[:controller]=self.name.gsub(/ControllerTest$/,'').tableizeendmatcher=route(method,path).to(options)shouldmatcher.descriptiondoassert_acceptsmatcher.in_context(self),selfendendendendend