Skip to content
Browse files

Removing application controller. Updating to 0.0.10.pre.3

  • Loading branch information...
1 parent 70c8860 commit 78605ec6e9c7d2d1c32db06625e93949ac94cdcb @gus gus committed
View
2 CHANGELOG
@@ -1,3 +1,5 @@
+*0.0.10.pre.3*
+
*0.0.9.pre*
* To accommodate the last change, added concept of context helpers. It's so simple you can define your own. [jaknowlden]
View
2 VERSION
@@ -1 +1 @@
-0.0.10.pre.2
+0.0.10.pre.3
View
8 lib/riot/action_controller.rb
@@ -1,8 +0,0 @@
-require 'action_controller/test_case'
-require 'action_view/test_case'
-
-require 'riot/rails'
-require 'riot/action_controller/context_macros'
-require 'riot/action_controller/assertion_macros'
-require 'riot/action_controller/situation_macros'
-require 'riot/action_controller/context_helper'
View
123 lib/riot/action_controller/assertion_macros.rb
@@ -1,123 +0,0 @@
-module RiotRails
- module ActionController
- # Asserts that calling body on whatever is passed in matches the expected value. Expected value can be a
- # literal string or a regular expression. It makes most sense to provide this macro with the response.
- #
- # rails_context UsersController do
- # hookup { get :index }
- # asserts(:response).renders("a bunch of html")
- # asserts(:response).renders(/bunch of/)
- # end
- class RendersMacro < Riot::AssertionMacro
- register :renders
-
- def evaluate(actual, expected)
- actual_body = actual.body
- if (expected.kind_of?(Regexp) ? (actual_body =~ expected) : (expected == actual_body))
- pass
- else
- verb = expected.kind_of?(Regexp) ? "match" : "equal"
- fail expected_message.response_body(actual_body).to.push("#{verb} #{expected.inspect}")
- end
- end
- end
-
- # Asserts that the name you provide is the basename of the rendered template. For instance, if you
- # expect the rendered template is named "foo_bar.html.haml" and you pass "foo_bar" into
- # renders_template, the assertion would pass. If instead you pass "foo" into renders_template, the
- # assertion will fail. Using Rails' assert_template both assertions would pass.
- #
- # It's important to note that the actual value provided must respond to +#rendered+. It's best to run
- # this assertion on the response
- #
- # rails_context ThingsController do
- # hookup { get :index }
- # asserts(:response).renders_template(:index)
- # asserts(:response).renders_template("index")
- # asserts(:response).renders_template("index.erb") # fails even if that's the name of the template
- # end
- class RendersTemplateMacro < Riot::AssertionMacro
- register :renders_template
-
- def evaluate(actual, expected_name)
- name = expected_name.to_s
- actual_template_path = Array(actual.template.rendered[:template]).map(&:inspect).first.to_s # yuck
- actual_template_name = File.basename(actual_template_path)
- if actual_template_name.to_s.match(%r[^#{name}(\.\w+)*$])
- pass new_message.renders_template(name)
- else
- fail expected_message.template(name).not(actual_template_path)
- end
- end
- end
-
- # Asserts that the HTTP response code equals your expectation. You can use the symbolized form of the
- # status code or the integer code itself. Not currently supporting status ranges; such as: +:success+,
- # +:redirect+, etc.
- #
- # It's important to note that the actual value provided must respond to +#response_code+. It's best to
- # run this assertion on the response
- #
- # asserts(:response).code(:ok)
- # asserts(:response).code(200)
- #
- # asserts(:response).code(:not_found)
- # asserts(:response).code(404)
- #
- # # A redirect
- # asserts(:response).code(:found)
- # asserts(:response).code(302)
- #
- # See +ActionController::StatusCodes+ for the list of available codes.
- class ResponseCodeMacro < Riot::AssertionMacro
- register :code
-
- def evaluate(actual, expected_code)
- if expected_code.kind_of?(Symbol)
- expected_code = ::Rack::Utils::SYMBOL_TO_STATUS_CODE[expected_code]
- end
- actual_code = actual.response_code
- if expected_code == actual_code
- pass("returns response code #{expected_code}")
- else
- fail("expected response code #{expected_code}, not #{actual_code}")
- end
- end
- end
-
- # Asserts that the response from an action is a redirect and that the path or URL matches your
- # expectations. If the response code is not in the 300s, the assertion will fail. If the ressponse code
- # is fine, but the redirect-to path or URL do not exactly match your expectation, the assertion will
- # fail.
- #
- # In order to use named routes which are handy, provide the path in a block. +redirected_to+ expects the
- # actual value to quack like a response object, which means it must respond to +response_code+ and
- # +redirected_to+.
- #
- # rails_context PeopleController do
- # hookup do
- # post :create, :person { ... }
- # end
- #
- # asserts(:response).redirected_to { person_path(...) }
- # end
- #
- # PS: There is a difference between saying +named_route_path+ and +named_route_url+ and Riot Rails will
- # be very strict (read: annoying) about it :)
- class RedirectedToMacro < Riot::AssertionMacro
- register :redirected_to
-
- def evaluate(actual, expected_redirect)
- actual_response_code = actual.response_code
- if (300...400).member?(actual_response_code)
- actual_redirect = actual.redirect_url
- msg = "expected to redirect to <#{expected_redirect}>, not <#{actual_redirect}>"
- expected_redirect == actual_redirect ? pass("redirected to #{expected_redirect}") : fail(msg)
- else
- fail("expected response to be a redirect, but was #{actual_response_code}")
- end
- end
- end
-
- end # ActionController
-end # RiotRails
View
12 lib/riot/action_controller/context_helper.rb
@@ -1,12 +0,0 @@
-module RiotRails
- register_context_helper do
- def prepare_context(context)
- context.controlling(context.description) if action_controller_context?(context)
- end
- private
- def action_controller_context?(context)
- description = context.description
- description.kind_of?(Class) && description.ancestors.include?(::ActionController::Base)
- end
- end
-end # RiotRails
View
93 lib/riot/action_controller/context_macros.rb
@@ -1,93 +0,0 @@
-module RiotRails #:nodoc:
- module ActionController #:nodoc:
-
- module ContextMacros
- # Sets up a context (and possibly child contexts) for testing a controller. Right now, it just takes a
- # symbol. Should change to allow you to pass in the class directly.
- #
- # rails_context "the FooBarsController" do
- # controlling :foo_bars
- # hookup { get :index }
- # end
- def controlling(controller_name)
- controller_class = constantize_controller_name(controller_name)
- premium_setup do
- controller_class.instance_eval { include ::ActionController::TestCase::RaiseActionExceptions }
- @request = ::ActionController::TestRequest.new
- @response = ::ActionController::TestResponse.new
- @controller = controller_class.new
- @controller.params = {}
- @controller.request = @request
- @controller
- end
- end
-
- # Creates a shortcut assertion that is to be used with the assertion macros for ActionController.
- #
- # rails_context "the FoosController" do
- # controlling :foos
- # hookup { get :index }
- #
- # asserts_controller.response_status(:found)
- # asserts_controller.redirected_to { new_foo_path }
- # end
- #
- # Works the same as if you wrote the following assertions:
- #
- # asserts("controller") { controller }.response_status(:found)
- # asserts("controller") { controller }.redirected_to { new_foo_path }
- def asserts_controller
- asserts("controller") { controller }
- end
-
- # Creates a shortcut assertion in order to test the response directly. If you provide an argument
- # it is assumed you want to call the relevant method on the response in order test it's returned value.
- #
- # rails_context "the FoosController" do
- # controlling :foos
- # hookup { get :index }
- #
- # asserts_response.kind_of(ActionController::TestResponse)
- # asserts_response(:body).kind_of(String)
- # end
- def asserts_response(shortcut_method=nil)
- asserts("the response") { shortcut_method ? response.send(shortcut_method) : response }
- end
-
- # Creates a shortcut assertion in order to test the request directly. If you provide an argument
- # it is assumed you want to call the relevant method on the request in order test it's returned value.
- #
- # rails_context "the FoosController" do
- # controlling :foos
- # hookup { get :index }
- #
- # asserts_request.kind_of(ActionController::TestRequest)
- # asserts_request(:cookies).kind_of(Hash)
- # end
- def asserts_request(shortcut_method=nil)
- asserts("the request") { shortcut_method ? request.send(shortcut_method) : request }
- end
-
- # Creates a shortcut assertion that returns the value of an assigned variable from the controller
- #
- # rails_context UsersController do
- # hookup { get :show, :id => 1 }
- #
- # asserts_assigned(:user).kind_of(User)
- # end
- def asserts_assigned(variable)
- asserts("value assigned to @#{variable}") do
- @controller.instance_variable_get("@#{variable}".to_sym)
- end
- end
- private
- def constantize_controller_name(name)
- name.kind_of?(Class) ? name : "#{name.to_s.camelize}Controller".constantize
- end
- end # ContextMacros
-
- # We do this everywhere because you never know when someone might define a rails_context that doesn't
- # automatically translate to an ActionController context
- RailsContext.instance_eval { include ContextMacros }
- end # ActionController
-end # RiotRails
View
78 lib/riot/action_controller/situation_macros.rb
@@ -1,78 +0,0 @@
-module RiotRails #:nodoc:
- module ActionController #:nodoc:
-
- # Nabbed form actionpack-3.0.0.beta
- module HttpSupport
- attr_reader :controller, :request, :response
-
- # Simulates a GET HTTP request and handles response
- def get(*args) submit_request("GET", *args); end
-
- # Simulates a POST HTTP request and handles response
- def post(*args) submit_request("POST", *args); end
-
- # Simulates a PUT HTTP request and handles response
- def put(*args) submit_request("PUT", *args); end
-
- # Simulates a DELETE HTTP request and handles response
- def delete(*args) submit_request("DELETE", *args); end
-
- # Simulates a HEAD HTTP request and handles response
- def head(*args) submit_request("HEAD", *args); end
-
- # Simulates an XML HTTP Request (which allows for AJAX ya'll) for whatever HTTP method you want
- def xml_http_request(request_method, *args)
- # @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
- # @request.env['HTTP_ACCEPT'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
- # returning submit_request(request_method.to_s.upcase, *args) do
- # @request.env.delete 'HTTP_X_REQUESTED_WITH'
- # @request.env.delete 'HTTP_ACCEPT'
- # end
- end
- alias :xhr :xml_http_request
- private
- def submit_request(http_method, action, parameters = nil, session = nil, flash = nil)
- @request.recycle!
- @response.recycle!
- @controller.response_body = nil
- @controller.formats = nil
- @controller.params = nil
-
- @html_document = nil
- @request.env['REQUEST_METHOD'] = http_method
-
- parameters ||= {}
- @request.assign_parameters(@controller.class.name.underscore.sub(/_controller$/, ''), action.to_s, parameters)
-
- @request.session = ActionController::TestSession.new(session) unless session.nil?
- @request.session["flash"] = @request.flash.update(flash || {})
- @request.session["flash"].sweep
-
- @controller.request = @request
- @controller.params.merge!(parameters)
- build_request_uri(action, parameters)
- ::ActionController::Base.class_eval { include ::ActionController::Testing }
- @controller.process_with_new_base_test(@request, @response)
- @request.session.delete('flash') if @request.session['flash'].blank?
- @response
- end
-
- def build_request_uri(action, parameters)
- unless @request.env['REQUEST_URI']
- options = @controller.__send__(:rewrite_options, parameters)
- options.update(:only_path => true, :action => action)
-
- url = ::ActionController::UrlRewriter.new(@request, parameters)
- @request.request_uri = url.rewrite(options)
- end
- end
- end # HttpSupport
-
- end # ActionController
-end # RiotRails
-
-Riot::Situation.instance_eval do
- include RiotRails::ActionController::HttpSupport
- include ::ActionController::UrlFor
- default_url_options[:host] = "test.host"
-end
View
54 test/action_controller/controller_context_test.rb
@@ -1,54 +0,0 @@
-require 'teststrap'
-
-class FoosController < ActionController::Base
- def index
- @thing = "hoo ray"
- render :text => ''
- end
-end
-
-rails_context "A controller test" do
- controlling :foos
-
- asserts_controller.kind_of(FoosController)
- asserts_topic.kind_of(FoosController)
-
- asserts("request is accessible") { request }.kind_of(::ActionDispatch::TestRequest)
- asserts("response is accessible") { response }.kind_of(::ActionDispatch::TestResponse)
- asserts("params is accessible") { controller.params }.equals({})
-
- asserts("http method shortcut for get") { self }.responds_to(:get)
- asserts("http method shortcut for post") { self }.responds_to(:post)
- asserts("http method shortcut for put") { self }.responds_to(:put)
- asserts("http method shortcut for delete") { self }.responds_to(:delete)
- asserts("http method shortcut for xml_http_request") { self }.responds_to(:xml_http_request)
- asserts("http method shortcut for xhr") { self }.responds_to(:xhr)
-
- asserts("an unknown action call") do
- get :burberry
- end.raises(::ActionController::UnknownAction, "The action 'burberry' could not be found")
-end # A controller test
-
-rails_context "A controller test using class as argument" do
- controlling FoosController
-
- asserts_controller.kind_of(FoosController)
-end # A controller test using class as argument
-
-rails_context FoosController do
- hookup do
- @actual_class_name = topic.class.name
- get :index
- end
-
- asserts("actual class name") { @actual_class_name }.equals("FoosController")
-
- asserts_assigned(:thing).equals("hoo ray")
- asserts_assigned(:that).nil
-
- asserts_response.kind_of(::ActionDispatch::TestResponse)
- asserts_response(:response_code).equals(200)
-
- asserts_request.kind_of(::ActionDispatch::TestRequest)
- asserts_request(:cookies).kind_of(Hash)
-end # A controller test with a hookup using the controller
View
36 test/action_controller/redirected_to_test.rb
@@ -1,36 +0,0 @@
-require 'teststrap'
-
-class RedirectedToController < ActionController::Base
- def index
- redirect_to new_gremlin_path
- end
-
- def show
- render :text => ""
- end
-end
-
-context "Asserting the redirect of an action" do
- setup do
- @situation = Riot::Situation.new
- context = RiotRails::RailsContext.new("redirected to") {}
- context.controlling(:redirected_to).run(@situation)
- context
- end
-
- context "when doing an actual redirect" do
- hookup_for_assertion_test { get :index }
-
- assertion_test_passes("when expected url matches actual redirect url", "redirected to http://test.host/gremlins/new") do
- topic.asserts(:response).redirected_to { new_gremlin_url }
- end
- end # when doing an actual redirect
-
- context "when not actually doing a redirect" do
- hookup_for_assertion_test { get :show }
- assertion_test_fails("with message about expecting a redirect",
- "expected response to be a redirect, but was 200") do
- topic.asserts(:response).redirected_to { new_gremlin_path }
- end
- end # when not actually doing a redirect
-end # Asserting the redirect of an action
View
57 test/action_controller/renders_template_test.rb
@@ -1,57 +0,0 @@
-require 'teststrap'
-
-class RenderedTemplatesController < ActionController::Base
- def foo_bar; end
- def text_me; render :text => "blah"; end
-end
-
-context "Asserting the rendered template for an action" do
- setup do
- @situation = Riot::Situation.new
- context = RiotRails::RailsContext.new("rendered_template") {}
- context.controlling(:rendered_templates).run(@situation)
- context
- end
-
- context "that rendered a template" do
- hookup_for_assertion_test { get :foo_bar }
-
- assertion_test_passes("when rendered template name matches expectation", %Q{renders template "foo_bar"}) do
- topic.asserts_controller.renders_template('foo_bar')
- end
-
- assertion_test_fails("when rendered template does not match expectation",
- %Q{expected template "bar_foo", not "app/views/rendered_templates/foo_bar.html.erb"}) do
- topic.asserts_controller.renders_template('bar_foo')
- end
- end # that rendered a template
-
- context "that did not render a template, as was expected" do
- hookup_for_assertion_test { get :text_me }
-
- assertion_test_passes("when providing nil as expectation", %Q{renders template ""}) do
- topic.asserts_controller.renders_template(nil)
- end
-
- assertion_test_passes("when providing empty string as expectation", %Q{renders template ""}) do
- topic.asserts_controller.renders_template("")
- end
- end # that did not render a template, as was expected
-
- context "that did not render a template but expected one" do
- hookup_for_assertion_test { get :text_me }
- assertion_test_fails("with message", %Q{expected template "text_me", not ""}) do
- topic.asserts_controller.renders_template('text_me')
- end
- end # that did not render a template but expected one
-
- context "that rendered a template with a partial match on template name" do
- hookup_for_assertion_test { get :foo_bar }
-
- assertion_test_fails("with message",
- %Q{expected template "foo", not "app/views/rendered_templates/foo_bar.html.erb"}) do
- topic.asserts_controller.renders_template('foo')
- end
- end # that rendered a template with a partial match on template name
-
-end # Asserting the rendered template for an action
View
30 test/action_controller/renders_test.rb
@@ -1,30 +0,0 @@
-require 'teststrap'
-
-class RendersController < ActionController::Base
- def index; render :text => "Yo mama"; end
-end
-
-context "Asserting the body of a response" do
- setup do
- @situation = Riot::Situation.new
- context = RiotRails::RailsContext.new("renders") {}
- context.controlling(:renders).run(@situation)
- context.hookup { get :index }.run(@situation)
- context
- end
-
- assertion_test_passes("when body equals expected") { topic.asserts(:response).renders("Yo mama") }
-
- assertion_test_fails("when rendered action body does not equal expected",
- %Q{expected response body "Yo mama" to equal "Yo"}) do
- topic.asserts(:response).renders("Yo")
- end
-
- assertion_test_passes("when body matches expected") { topic.asserts(:response).renders(/mama/) }
-
- assertion_test_fails("when rendered action body does not match expected",
- %Q{expected response body "Yo mama" to match /obama/}) do
- topic.asserts(:response).renders(/obama/)
- end
-
-end # Asserting the body of a response
View
57 test/action_controller/response_status_test.rb
@@ -1,57 +0,0 @@
-require 'teststrap'
-
-class ResponseCodesController < ActionController::Base
- def ok_go; render :text => ""; end
- def fffound; redirect_to "http://your.momshou.se"; end
- def make_me; render :text => "", :status => 201; end
-end
-
-context "Asserting the response status for an action" do
- setup do
- @situation = Riot::Situation.new
- context = RiotRails::RailsContext.new("response status") {}
- context.controlling(:response_codes).run(@situation)
- context
- end
-
- context "returning OK" do
- hookup_for_assertion_test { get :ok_go }
-
- assertion_test_passes("when asked if :ok", "returns response code 200") do
- topic.asserts(:response).code(:ok)
- end
-
- assertion_test_passes("when asked if 200", "returns response code 200") do
- topic.asserts(:response).code(200)
- end
-
- assertion_test_fails("when CONTINUE returned instead", %Q{expected response code 100, not 200}) do
- topic.asserts(:response).code(100)
- end
- end # returning OK
-
- context "that is redirecting" do
- hookup_for_assertion_test { get :fffound }
-
- assertion_test_passes("when asked if :found", "returns response code 302") do
- topic.asserts(:response).code(:found)
- end
-
- assertion_test_passes("when asked if 302", "returns response code 302") do
- topic.asserts(:response).code(302)
- end
- end # that is redirecting
-
- context "that has explicit status" do
- hookup_for_assertion_test { get :make_me }
-
- assertion_test_passes("when asked if :created", "returns response code 201") do
- topic.asserts(:response).code(:created)
- end
-
- assertion_test_passes("when asked if 201", "returns response code 201") do
- topic.asserts(:response).code(201)
- end
- end # that has explicit status
-
-end # Asserting the status of a response

0 comments on commit 78605ec

Please sign in to comment.
Something went wrong with that request. Please try again.