I have a Controller:
class ThingController < ActionController
respond_to :json
def create
puts "CREATE " + params.inspect
end
end
and a test:
require "spec_helper"
describe "/thing" do
context "create" do
it "should get params" do
params = {"a" => "b", "c" => ["d"], "e" => [], "f"=>"",
"g"=>nil, , "controller" => "NOPE", "action" => "NOPE"}
post uri, params
end
end
end
When I run this, the following is logged:
CREATE {"a"=>"b", "c"=>["d"], "action"=>"create", "controller"=>"thing"}
My questions are:
where did e go? I would expect it to deserialize to an empty array, not to nothing at all.
why are the action and controller params being mixed into this? Aren't the body and the rails internals completely separate concerns?
Because of this, my action and controller JSON fields were over-written. How would I access these?
Is this therefore not the right way to accept JSON?
I'm new to Rails, but I have done a lot of Django.
There are two parts to this problem: you need to ensure that your parameters are being sent as JSON, and also that they are being interpreted as JSON.
Essentially, you have to
encode your parameters as JSON
set appropriate content-type and accepts headers
See POSTing raw JSON data with Rails 3.2.11 and RSpec for the way.
The rails middleware will add the action and controller params so you'll have to put those in a nested hash if you still want to access your custom values.
Try adding format: 'json' to the params in your test. This will send a different content-type header and might help serialize the params correctly in order to keep the e param.
I'm trying to test a controller that's using an http token authentication in the before filter. My problem is that it works ok wheh I use curl to pass the token, but in my tests it always fails (I'm using rspec btw). Tried a simple test to see if the token was being passed at all, but it seems like it's not doing so. Am I missing anything to get the test to actually pass the token to the controller?
Here's my before filter:
def restrict_access
authenticate_or_request_with_http_token do |token, options|
api_key = ApiKey.find_by_access_token(token)
#user = api_key.user unless api_key.nil?
#token = token #set just for the sake of testing
!api_key.nil?
end
end
And here is my test:
it "passes the token" do
get :new, nil,
:authorization => ActionController::HttpAuthentication::Token.encode_credentials("test_access1")
assigns(:token).should be "test_access1"
end
I'm assuming ApiKey is an ActiveRecord model, correct? curl command runs against development database, and tests go against test db. I can't see anything that sets up ApiKey in your snippets. Unless you have it somewhere else, try adding something along these lines:
it "passes the token" do
# use factory or just create record with AR:
ApiKey.create!(:access_token => 'test_access1', ... rest of required attributes ...)
# this part remains unchanged
get :new, nil,
:authorization => ActionController::HttpAuthentication::Token.encode_credentials("test_access1")
assigns(:token).should be "test_access1"
end
You can later move it to before :each block or support module.
UPDATE:
After seeing your comment I had to look deeper. Here's another guess. This form of get
get '/path', nil, :authorization => 'string'
should work only in integration tests. And for controller tests auth preparation should look like this:
it "passes the token" do
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials("test_access1")
get :new
assigns(:token).should be "test_access1"
end
Reasons behind this come from method signatures for respective test modules:
# for action_controller/test_case.rb
def get(action, parameters = nil, session = nil, flash = nil)
# for action_dispatch/testing/integration.rb
def get(path, parameters = nil, headers = nil)
I'm writing RSpec integration tests as I convert my spaghetti code to use accepts_nested_attributes_for. I have a snippet like this:
# file: spec/requests/wizard_spec.rb
describe 'POST /wizard with address' do
before(:each) do
#premise_attributes = {
"address"=>"600 Mellow Ave, Mellow Park, CA 94025, USA",
}
end
it 'should succeed' do
post :create, "wizard" => { "premise_attributes" => #premise_attributes }
response.status.should be(200)
end
end
Of course, this fails with:
Failure/Error: post :create, "wizard" => { "premise_attributes" => #premise_attributes }
ArgumentError:
bad argument(expected URI object or URI string)
Is there a method that converts the nested attributes hashes into a POST-able format?
(Related but less important: where is the post method documented or defined? I'd like to see what it really accepts as arguments.)
Instead post :create try use post "/wizard" or nest your specs inside describe WizardController do; end block. Generally you can use method :action syntax only if you're inside describe block for the given controller.
I found this while trying to fix my issue with trying to test put. My post method works though so maybe I can help you out if you still need it. I think your issue is that you're trying to update your attributes as if it was a scalar type variable, but nested attributes are really like an array. Rails generally names them "0", "1", etc., but I'm not sure it matters what the names are as long as they're unique. Give this a try:
#premise_attributes = {
"0" => {"address"=>"600 Mellow Ave, Mellow Park, CA 94025, USA"}
}
(By the way, the problem I'm having is that my update specs are failing because it says something like my address is not unique to borrow your example.)
I have a action in some controller that set some value in a permanent signed cookie like this:
def some_action
cookies.permanent.signed[:cookie_name] = "somevalue"
end
And in some functional test, I'm trying to test if the cookie was set correctly suing this:
test "test cookies" do
assert_equal "somevalue", cookies.permanent.signed[:cookie_name]
end
However, when I run the test, I got the following error:
NoMethodError: undefined method `permanent' for #
If I try only:
test "test cookies" do
assert_equal "somevalue", cookies.signed[:cookie_name]
end
I get:
NoMethodError: undefined method `signed' for #
How to test signed cookies in Rails 3?
I came across this question while Googling for a solution to a similar issue, so I'll post here. I was hoping to set a signed cookie in Rspec before testing a controller action. The following worked:
jar = ActionDispatch::Cookies::CookieJar.build(#request)
jar.signed[:some_key] = "some value"
#request.cookies['some_key'] = jar[:some_key]
get :show ...
Note that the following didn't work:
# didn't work; the controller didn't see the signed cookie
#request.cookie_jar.signed[:some_key] = "some value"
get :show ...
In rails 3's ActionControlller::TestCase, you can set signed permanent cookies in the request object like so -
#request.cookies.permanent.signed[:foo] = "bar"
And the returned signed cookies from an action taken in a controller can be tested by doing this
test "do something" do
get :index # or whatever
jar = #request.cookie_jar
jar.signed[:foo] = "bar"
assert_equal jar[:foo], #response.cookies['foo'] #should both be some enc of 'bar'
end
Note that we need to set signed cookie jar.signed[:foo], but read unsigned cookie jar[:foo]. Only then we get the encrypted value of cookie, needed for comparison in assert_equal.
After looking at the Rails code that handles this I created a test helper for this:
def cookies_signed(name, opts={})
verifier = ActiveSupport::MessageVerifier.new(request.env["action_dispatch.secret_token".freeze])
if opts[:value]
#request.cookies[name] = verifier.generate(opts[:value])
else
verifier.verify(cookies[name])
end
end
Add this to test_help.rb, then you can set a signed cookie with:
cookies_signed(:foo, :value => 'bar')
And read it with:
cookies_signed(:foo)
A bit hackish maybe, but it does the job for me.
The problem (at least on the surface) is that in the context of a functional test (ActionController::TestCase), the "cookies" object is a Hash, whereas when you work with the controllers, it's a ActionDispatch::Cookies::CookieJar object. So we need to convert it to a CookieJar object so that we can use the "signed" method on it to convert it to a SignedCookieJar.
You can put the following into your functional tests (after a get request) to convert cookies from a Hash to a CookieJar object
#request.cookies.merge!(cookies)
cookies = ActionDispatch::Cookies::CookieJar.build(#request)
The problem also appears to be your tests.
Here is some code and tests I used to TDD the situation where you want to set a cookie's value from passing a params value into a view.
Functional Test:
test "reference get set in cookie when visiting the site" do
get :index, {:reference => "121212"}
refute_nil cookies["reference"]
end
SomeController:
before_filter :get_reference_code
ApplicationController:
def get_reference_code
cookies.signed[:reference] ||= params[:reference]
end
Notice that the refute_nil line, the cookies is a string... that is one thing that also made this test not pass, was putting a symbol in cookies[:reference] the test did not like that, so i didn't do that.
When I load script/console, sometimes I want to play with the output of a controller or a view helper method.
Are there ways to:
simulate a request?
call methods from a controller instance on said request?
test helper methods, either via said controller instance or another way?
To call helpers, use the helper object:
$ ./script/console
>> helper.number_to_currency('123.45')
=> "R$ 123,45"
If you want to use a helper that's not included by default (say, because you removed helper :all from ApplicationController), just include the helper.
>> include BogusHelper
>> helper.bogus
=> "bogus output"
As for dealing with controllers, I quote Nick's answer:
> app.get '/posts/1'
> response = app.response
# you now have a rails response object much like the integration tests
> response.body # get you the HTML
> response.cookies # hash of the cookies
# etc, etc
An easy way to call a controller action from a script/console and view/manipulate the response object is:
> app.get '/posts/1'
> response = app.response
# You now have a Ruby on Rails response object much like the integration tests
> response.body # Get you the HTML
> response.cookies # Hash of the cookies
# etc., etc.
The app object is an instance of ActionController::Integration::Session
This works for me using Ruby on Rails 2.1 and 2.3, and I did not try earlier versions.
If you need to test from the console (tested on Ruby on Rails 3.1 and 4.1):
Call Controller Actions:
app.get '/'
app.response
app.response.headers # => { "Content-Type"=>"text/html", ... }
app.response.body # => "<!DOCTYPE html>\n<html>\n\n<head>\n..."
ApplicationController methods:
foo = ActionController::Base::ApplicationController.new
foo.public_methods(true||false).sort
foo.some_method
Route Helpers:
app.myresource_path # => "/myresource"
app.myresource_url # => "http://www.example.com/myresource"
View Helpers:
foo = ActionView::Base.new
foo.javascript_include_tag 'myscript' #=> "<script src=\"/javascripts/myscript.js\"></script>"
helper.link_to "foo", "bar" #=> "foo"
ActionController::Base.helpers.image_tag('logo.png') #=> "<img alt=\"Logo\" src=\"/images/logo.png\" />"
Render:
views = Rails::Application::Configuration.new(Rails.root).paths["app/views"]
views_helper = ActionView::Base.new views
views_helper.render 'myview/mytemplate'
views_helper.render file: 'myview/_mypartial', locals: {my_var: "display:block;"}
views_helper.assets_prefix #=> '/assets'
ActiveSupport methods:
require 'active_support/all'
1.week.ago
=> 2013-08-31 10:07:26 -0300
a = {'a'=>123}
a.symbolize_keys
=> {:a=>123}
Lib modules:
> require 'my_utils'
=> true
> include MyUtils
=> Object
> MyUtils.say "hi"
evaluate: hi
=> true
Here's one way to do this through the console:
>> foo = ActionView::Base.new
=> #<ActionView::Base:0x2aaab0ac2af8 #assigns_added=nil, #assigns={}, #helpers=#<ActionView::Base::ProxyModule:0x2aaab0ac2a58>, #controller=nil, #view_paths=[]>
>> foo.extend YourHelperModule
=> #<ActionView::Base:0x2aaab0ac2af8 #assigns_added=nil, #assigns={}, #helpers=#<ActionView::Base::ProxyModule:0x2aaab0ac2a58>, #controller=nil, #view_paths=[]>
>> foo.your_helper_method(args)
=> "<html>created by your helper</html>"
Creating a new instance of ActionView::Base gives you access to the normal view methods that your helper likely uses. Then extending YourHelperModule mixes its methods into your object letting you view their return values.
If the method is the POST method then:
app.post 'controller/action?parameter1=value1¶meter2=value2'
(Here parameters will be as per your applicability.)
Else if it is the GET method then:
app.get 'controller/action'
Here is how to make an authenticated POST request, using Refinery as an example:
# Start Rails console
rails console
# Get the login form
app.get '/community_members/sign_in'
# View the session
app.session.to_hash
# Copy the CSRF token "_csrf_token" and place it in the login request.
# Log in from the console to create a session
app.post '/community_members/login', {"authenticity_token"=>"gT7G17RNFaWUDLC6PJGapwHk/OEyYfI1V8yrlg0lHpM=", "refinery_user[login]"=>'chloe', 'refinery_user[password]'=>'test'}
# View the session to verify CSRF token is the same
app.session.to_hash
# Copy the CSRF token "_csrf_token" and place it in the request. It's best to edit this in Notepad++
app.post '/refinery/blog/posts', {"authenticity_token"=>"gT7G17RNFaWUDLC6PJGapwHk/OEyYfI1V8yrlg0lHpM=", "switch_locale"=>"en", "post"=>{"title"=>"Test", "homepage"=>"0", "featured"=>"0", "magazine"=>"0", "refinery_category_ids"=>["1282"], "body"=>"Tests do a body good.", "custom_teaser"=>"", "draft"=>"0", "tag_list"=>"", "published_at(1i)"=>"2014", "published_at(2i)"=>"5", "published_at(3i)"=>"27", "published_at(4i)"=>"21", "published_at(5i)"=>"20", "custom_url"=>"", "source_url_title"=>"", "source_url"=>"", "user_id"=>"56", "browser_title"=>"", "meta_description"=>""}, "continue_editing"=>"false", "locale"=>:en}
You might find these useful too if you get an error:
app.cookies.to_hash
app.flash.to_hash
app.response # long, raw, HTML
Another way to do this is to use the Ruby on Rails debugger. There's a Ruby on Rails guide about debugging at http://guides.rubyonrails.org/debugging_rails_applications.html
Basically, start the server with the -u option:
./script/server -u
And then insert a breakpoint into your script where you would like to have access to the controllers, helpers, etc.
class EventsController < ApplicationController
def index
debugger
end
end
And when you make a request and hit that part in the code, the server console will return a prompt where you can then make requests, view objects, etc. from a command prompt. When finished, just type 'cont' to continue execution. There are also options for extended debugging, but this should at least get you started.
You can access your methods in the Ruby on Rails console like the following:
controller.method_name
helper.method_name
In Ruby on Rails 3, try this:
session = ActionDispatch::Integration::Session.new(Rails.application)
session.get(url)
body = session.response.body
The body will contain the HTML of the URL.
How to route and render (dispatch) from a model in Ruby on Rails 3
The earlier answers are calling helpers, but the following will help for calling controller methods. I have used this on Ruby on Rails 2.3.2.
First add the following code to your .irbrc file (which can be in your home directory)
class Object
def request(options = {})
url=app.url_for(options)
app.get(url)
puts app.html_document.root.to_s
end
end
Then in the Ruby on Rails console you can type something like...
request(:controller => :show, :action => :show_frontpage)
...and the HTML will be dumped to the console.
For controllers, you can instantiate a controller object in the Ruby on Rails console.
For example,
class CustomPagesController < ApplicationController
def index
#customs = CustomPage.all
end
def get_number
puts "Got the Number"
end
protected
def get_private_number
puts 'Got private Number'
end
end
custom = CustomPagesController.new
2.1.5 :011 > custom = CustomPagesController.new
=> #<CustomPagesController:0xb594f77c #_action_has_layout=true, #_routes=nil, #_headers={"Content-Type"=>"text/html"}, #_status=200, #_request=nil, #_response=nil>
2.1.5 :014 > custom.get_number
Got the Number
=> nil
# For calling private or protected methods,
2.1.5 :048 > custom.send(:get_private_number)
Got private Number
=> nil
Inside any controller action or view, you can invoke the console by calling the console method.
For example, in a controller:
class PostsController < ApplicationController
def new
console
#post = Post.new
end
end
Or in a view:
<% console %>
<h2>New Post</h2>
This will render a console inside your view. You don't need to care about the location of the console call; it won't be rendered on the spot of its invocation but next to your HTML content.
See: http://guides.rubyonrails.org/debugging_rails_applications.html
One possible approach for Helper method testing in the Ruby on Rails console is:
Struct.new(:t).extend(YourHelper).your_method(*arg)
And for reload do:
reload!; Struct.new(:t).extend(YourHelper).your_method(*arg)
If you have added your own helper and you want its methods to be available in console, do:
In the console execute include YourHelperName
Your helper methods are now available in console, and use them calling method_name(args) in the console.
Example: say you have MyHelper (with a method my_method) in 'app/helpers/my_helper.rb`, then in the console do:
include MyHelper
my_helper.my_method