Permalink
Browse files

Some basic GET support

  • Loading branch information...
1 parent ead59e4 commit 4d7d7be177af77e2b0eb55763b979653702d9e2c @gus gus committed May 14, 2010
@@ -1,2 +1,3 @@
require 'riot/rails'
require 'riot/action_controller/context_helper'
+require 'riot/action_controller/http_methods'
@@ -1,14 +1,27 @@
RiotRails.register_context_helper do
def prepare_context(context)
if action_controller_context?(context)
+
+ context.helper(:app) { @app }
+ context.helper(:env) { @env }
+ context.helper(:controller) { @controller }
+ context.helper(:request) { @request }
+ context.helper(:response) { @response }
+
context.premium_setup do
+ self.class_eval do
+ include Rack::Test::Methods
+ include RiotRails::ActionController::HttpMethods
+ 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
end
+
end
end
private
@@ -0,0 +1,22 @@
+module RiotRails
+ 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)
+ 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]]
+ end
+ end # HttpMethods
+ end # ActionController
+end # RiotRails
@@ -13,11 +13,26 @@
end
asserts(:topic).equals(RoomsController)
+
asserts("@app") { assigned(:@app) }.equals(RoomsController)
+ asserts("helper app") { topic.app }.equals { assigned(:@app) }
+
asserts("@env") { assigned(:@env) }.equals({"rack.session" => {}})
+ asserts("helper env") { topic.env }.equals { assigned(:@env) }
+
asserts("@controller") { assigned(:@controller) }.kind_of(RoomsController)
+ 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("@response") { assigned(:@response) }.kind_of(Rack::Response)
+ asserts("helper response") { topic.response }.equals { assigned(:@response) }
+
+ asserts_topic.responds_to(:get)
+ asserts_topic.responds_to(:post)
+ asserts_topic.responds_to(:put)
+ asserts_topic.responds_to(:delete)
end # for an ActionController class
end # The rails_context macro
@@ -0,0 +1,23 @@
+require 'teststrap'
+
+rails_context RoomsController do
+ context "for a GET request" do
+ setup do
+ get "/rooms/index"
+ end
+
+ asserts("action name") { env['action_dispatch.request.path_parameters'][:action] }.equals("index")
+
+ asserts("response status") { response.status }.equals(200)
+ asserts("response header") { response.header }.equals("Content-Type" => "text/html")
+ asserts("response body") { response.body }.equals("foo")
+ end
+
+ asserts("unknown action for existing controller") do
+ get "/rooms/blah"
+ end.raises(AbstractController::ActionNotFound)
+
+ asserts("controller does not the one under test") do
+ get "/gremlins/index"
+ end.raises(RiotRails::ActionController::ControllerMismatch, "Expected rooms controller, not gremlins")
+end # RoomsController
@@ -0,0 +1,3 @@
+class GremlinsController < ActionController::Base
+ def index; render :text => "bar"; end
+end
@@ -0,0 +1,3 @@
+class RoomsController < ActionController::Base
+ def index; render :text => "foo"; end
+end
View
@@ -1,11 +1,12 @@
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
require 'rubygems'
+require 'pathname'
#
# Setup faux Rails environment
-require File.join(File.dirname(__FILE__), 'rails_root', "config", "environment")
+require(Pathname(__FILE__).dirname + 'rails_root' + 'config' + 'environment')
#
# Model definition
@@ -29,12 +30,6 @@ def self.create_with_good_data(attributes={})
end
#
-# Controller definition
-
-class RoomsController < ActionController::Base
-end
-
-#
# Blah == anything, whatever. Always passes an equality test
class Blah

0 comments on commit 4d7d7be

Please sign in to comment.