Browse files

Using a much simplified approach to calling actions in controller tests

  • Loading branch information...
1 parent 2f35273 commit e5bc712334d86eb00f12d4fbd34f8584b8ca9c76 @gus gus committed Jun 1, 2010
View
2 .gitignore
@@ -1,2 +1,4 @@
test/rails_root/log/*
pkg/*
+riot-rails.tmproj
+test/rails_root/db/*.db
View
29 lib/riot/action_controller/context_middleware.rb
@@ -21,22 +21,23 @@ def setup_context_macros(context)
def setup_situation(context)
context.helper(:app) { @app }
context.helper(:env) { @env }
- context.helper(:controller) { @controller }
- context.helper(:request) { @request }
- context.helper(:response) { last_response }
+ context.helper(:controller) { env["action_controller.instance"] }
- context.setup(true) do
- self.class_eval do
- include Rack::Test::Methods
- include RiotRails::ActionController::HttpMethods
- end
+ context.helper(:request) do
+ return controller.request if controller
+ raise Exception, "No request made yet"
+ end
- @env = {}
- @controller = context.description.new.tap do |controller|
- controller.request = @request = Rack::Request.new(@env).tap { |rq| rq.session }
- @response = Rack::Response.new
- end
- @app = context.description
+ context.helper(:response) do
+ return controller.response if controller
+ raise Exception, "No response since no request made yet"
+ end
+
+ context.setup(true) do
+ self.class_eval { include RiotRails::ActionController::HttpMethods }
+ @app = ::Rails.application
+ http_reset
+ context.description
end # context.setup(true)
end # call
View
30 lib/riot/action_controller/http_methods.rb
@@ -3,20 +3,28 @@ module ActionController
class ControllerMismatch < Exception; end
module HttpMethods
- def get(uri, params={}, &block)
- env['action_dispatch.request.path_parameters'] = {:action => action_for_uri(uri)}
- current_session.get(uri, params, env, &block)
+ def http_reset
+ @env = {}
end
- private
- def action_for_uri(uri)
- controller_name = controller.controller_name
- recognized_path = Rails.application.routes.recognize_path(uri)
-
- return recognized_path[:action] if controller_name == recognized_path[:controller]
- error_msg = "Expected %s controller, not %s"
- raise ControllerMismatch, error_msg % [controller_name, recognized_path[:controller]]
+ def get(uri, params={}, &block)
+ http_reset
+ @env = ::Rack::MockRequest.env_for(uri, {:params => params})
+ @env['action_dispatch.show_exceptions'] = false
+ @app.call(@env)
end
+
+ def post; raise Exception, "POST isn't ready yet"; end
+ def put; raise Exception, "PUT isn't ready yet"; end
+ def delete; raise Exception, "DELETE isn't ready yet"; end
end # HttpMethods
end # ActionController
+
+ module RackRequest
+ def self.included(base)
+ base.class_eval { alias :parameters :params }
+ end
+ end
end # RiotRails
+
+Rack::Request.instance_eval { include RiotRails::RackRequest }
View
70 test/action_controller/context_middleware_test.rb
@@ -5,32 +5,62 @@
helper(:assigned) { |var| topic.instance_variable_get(var) }
- setup do
- situation = Riot::Situation.new
- topic.context(RoomsController) {}.local_run(Riot::SilentReporter.new, situation)
- situation
- end
+ context "pre-request state" do
+ setup do
+ situation = Riot::Situation.new
+ topic.context(RoomsController) {}.local_run(Riot::SilentReporter.new, situation)
+ situation
+ end
- asserts(:topic).equals(RoomsController)
+ asserts(:topic).equals(RoomsController)
- asserts("@app") { assigned(:@app) }.equals(RoomsController)
- asserts("helper app") { topic.app }.equals { assigned(:@app) }
+ asserts("@app") { assigned(:@app).to_s }.equals { ::Rails.application.to_s }
+ asserts("helper app") { topic.app.to_s }.equals { assigned(:@app).to_s }
- asserts("@env") { assigned(:@env) }.equals({"rack.session" => {}})
- asserts("helper env") { topic.env }.equals { assigned(:@env) }
+ asserts("@env") { assigned(:@env) }.equals({})
+ asserts("helper env") { topic.env }.equals { assigned(:@env) }
- asserts("@controller") { assigned(:@controller) }.kind_of(RoomsController)
- asserts("helper controller") { topic.controller }.equals { assigned(:@controller) }
+ asserts("helper controller") { topic.controller }.equals { assigned(:@controller) }
- asserts("@request") { assigned(:@request) }.kind_of(Rack::Request)
- asserts("helper request") { topic.request }.equals { assigned(:@request) }
+ asserts("request before any request made") do
+ topic.request
+ end.raises(Exception, "No request made yet")
- asserts("@response") { assigned(:@response) }.kind_of(Rack::Response)
- asserts("helper response when no request made") { topic.response }.raises(Rack::Test::Error)
+ asserts("response before any request made") do
+ topic.response
+ end.raises(Exception, "No response since no request made yet")
- asserts_topic.responds_to(:get)
- asserts_topic.responds_to(:post)
- asserts_topic.responds_to(:put)
- asserts_topic.responds_to(:delete)
+ asserts_topic.responds_to(:get)
+ asserts_topic.responds_to(:post)
+ asserts_topic.responds_to(:put)
+ asserts_topic.responds_to(:delete)
+ end # pre-request state
+
+ context "post-request state" do
+ setup do
+ situation = Riot::Situation.new
+ topic.context(RoomsController) do
+ setup { get("/rooms") }
+ end.local_run(Riot::SilentReporter.new, situation)
+ situation
+ end
+
+ asserts(:topic).kind_of(Array)
+ asserts(:topic).size(3)
+ asserts("#topic first element response status") { topic.topic[0] }.equals(200)
+ asserts("#topic second element") { topic.topic[1] }.kind_of(Hash)
+ asserts("#topic second element") { topic.topic[1] }.includes("ETag")
+ asserts("#topic last element") { topic.topic[2] }.kind_of(ActionDispatch::Response)
+
+ asserts("helper env") { topic.env }.includes("action_controller.instance")
+
+ asserts("helper controller") { topic.controller }.kind_of(RoomsController)
+ asserts("env['action_controller.instance']") do
+ topic.env["action_controller.instance"]
+ end.kind_of(RoomsController)
+
+ asserts("helper request") { topic.request }.kind_of(Rack::Request)
+ asserts("helper response") { topic.response }.kind_of(Rack::Response)
+ end # post-request state
end # ActionController middleware
View
12 test/action_controller/get_request_test.rb
@@ -4,21 +4,21 @@
asserts("unknown action for existing controller") do
get "/rooms/blah"
- end.raises(AbstractController::ActionNotFound)
+ end.raises(AbstractController::ActionNotFound, "The action 'blah' could not be found")
- asserts("controller does not the one under test") do
- get "/gremlins/index"
+ asserts("controller does not match the one under test") do
+ get "/gremlins/2"
end.raises(RiotRails::ActionController::ControllerMismatch, "Expected rooms controller, not gremlins")
context "for a GET request" do
helper(:action_name) { env['action_dispatch.request.path_parameters'][:action] }
context "without with parameters" do
- setup { get "/rooms/index" }
+ setup { get "/rooms" }
asserts("action name") { action_name }.equals("index")
asserts("response status") { response.status }.equals(200)
- asserts("response body") { last_response.body }.equals("foo")
+ asserts("response body") { response.body }.equals("foo")
context "response headers" do
setup { response.headers.keys }
@@ -32,7 +32,7 @@
setup { get "/rooms/echo_params", {:name => "Juiceton", :foo => "blaz"} }
asserts("action name") { action_name }.equals("echo_params")
- asserts("response body") { response.body }.equals("foo=blaz,name=Juiceton")
+ asserts("response body") { response.body }.equals("controller=rooms,foo=blaz,name=Juiceton")
end # with with parameters
end # for a GET request
View
16 test/action_controller/nested_resource_test.rb
@@ -0,0 +1,16 @@
+require 'teststrap'
+
+context "GET request for a nested RESTful resource" do
+ setup do
+ situation = Riot::Situation.new
+ context = Riot::Context.new(PartiesController) {}
+ context.asserts("stuff") { get("/gremlins/1/parties/2") }
+ context.local_run(Riot::SilentReporter.new, situation)
+ situation
+ end
+
+ asserts("parameters inferred from the URL") do
+ topic.request.params
+ end.equals({ "controller" => "parties", "action" => "show", "gremlin_id" => "1", "id" => "2" })
+
+end
View
5 test/rails_root/app/controllers/parties_controller.rb
@@ -0,0 +1,5 @@
+class PartiesController < ActionController::Base
+ def show
+ render :text => "woot"
+ end
+end
View
2 test/rails_root/config/database.yml
@@ -1,4 +1,4 @@
test:
adapter: "sqlite3"
- database: ":memory:"
+ database: <%= Rails.root + "db" + "test.db" %>
View
1 test/rails_root/config/environment.rb
@@ -11,7 +11,6 @@ class Application < Rails::Application
config.root = File.expand_path(File.join(File.dirname(__FILE__), ".."))
config.session_store :cookie_store, {:key => "_riotrails_session"}
- # config.secret_token = "i own you." * 3
config.cookie_secret = "i own you." * 3
end
end
View
5 test/rails_root/config/routes.rb
@@ -1,4 +1,7 @@
RiotRails::Application.routes.draw do |map|
- resources :gremlins
+ resources :gremlins do
+ resources :parties
+ end
+
match ':controller(/:action(/:id(.:format)))'
end

0 comments on commit e5bc712

Please sign in to comment.