Permalink
Browse files

Support query strings in params_from

[#652 state:resolved]
  • Loading branch information...
1 parent 4cec8c5 commit c3cd9fc2e8c6ec82258bae636d58eb8d8756efaa @wilson wilson committed with Jan 14, 2009
View
@@ -2,10 +2,11 @@
IMPORTANT: rspec-rails supports rails 2.0.2, 2.1.2 and 2.2.2. We are no longer supporting 1.x versions of rails.
-* 2 enhancements
+* 3 enhancements
* Adding status codes to redirect_to matcher (Damian Janowski). Closes #570.
* Initialize current URL before executing any examples in a ViewExampleGroup (Wilson Bilkovich). Closes #654.
+ * Support query strings in params_from (Wilson Bilkovich). Closes #652.
=== Version 1.1.12 / 2009-01-11
@@ -129,11 +129,13 @@ def route_for(options)
# Uses ActionController::Routing::Routes to parse
# an incoming path so the parameters it generates can be checked
# == Example
- # params_from(:get, '/registrations/1;edit')
+ # params_from(:get, '/registrations/1/edit')
# => :controller => 'registrations', :action => 'edit', :id => 1
def params_from(method, path)
ensure_that_routes_are_loaded
- ActionController::Routing::Routes.recognize_path(path, :method => method)
+ path, querystring = path.split('?')
+ params = ActionController::Routing::Routes.recognize_path(path, :method => method)
+ querystring.blank? ? params : params.merge(params_from_querystring(querystring))
end
protected
@@ -144,6 +146,15 @@ def _assigns_hash_proxy
end
private
+ def params_from_querystring(querystring)
+ params = {}
+ querystring.split('&').each do |piece|
+ key, value = piece.split('=')
+ params[key.to_sym] = value
+ end
+ params
+ end
+
def ensure_that_routes_are_loaded
ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
end
@@ -215,31 +215,45 @@
route_for(:controller => "controller_spec", :action => "some_action").should == "/controller_spec/some_action"
end
+ it "should support existing routes with additional parameters" do
+ route_for(:controller => "controller_spec", :action => "some_action", :param => '1').should == "/controller_spec/some_action?param=1"
+ end
+
it "should generate params for custom routes" do
params_from(:get, '/custom_route').should == {:controller => "custom_route_spec", :action => "custom_route"}
end
-
+
it "should generate params for existing routes" do
params_from(:get, '/controller_spec/some_action').should == {:controller => "controller_spec", :action => "some_action"}
end
-
+
+ it "should generate params for an existing route with a query parameter" do
+ expected = {:controller => "controller_spec", :action => "some_action", :param => '1'}
+ params_from(:get, '/controller_spec/some_action?param=1').should == expected
+ end
+
+ it "should generate params for an existing route with multiple query parameters" do
+ expected = {:controller => "controller_spec", :action => "some_action", :param1 => '1', :param2 => '2' }
+ params_from(:get, '/controller_spec/some_action?param1=1&param2=2').should == expected
+ end
+
it "should expose instance vars through the assigns hash" do
get 'action_setting_the_assigns_hash'
assigns[:indirect_assigns_key].should == :indirect_assigns_key_value
end
-
+
it "should expose instance vars through the assigns hash that are set to false" do
get 'action_that_assigns_false_to_a_variable'
assigns[:a_variable].should be_false
end
-
+
it "should NOT complain when calling should_receive with arguments other than :render" do
controller.should_receive(:anything_besides_render)
lambda {
controller.rspec_verify
}.should raise_error(Exception, /expected :anything_besides_render/)
end
-
+
it "should not run a skipped before_filter" do
lambda {
get 'action_with_skipped_before_filter'

0 comments on commit c3cd9fc

Please sign in to comment.