GET リクエストの場合
# bin/rails console
> app.get '/users/new'
Started GET "/users/new" for 127.0.0.1 at 2014-04-10 16:25:28 +0900
ActiveRecord::SchemaMigration Load (0.5ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by UsersController#new as HTML
Rendered shared/_error_messages.html.erb (4.7ms)
Rendered users/new.html.erb within layouts/application (34.7ms)
Completed 200 OK in 183ms (Views: 167.2ms | ActiveRecord: 0.3ms)
=> 200
> app.request.params
=> {"action"=>"new", "controller"=>"users"}
> app.request.headers
=> #<ActionDispatch::Http::Headers:0x007f8818eb2bf0 @env= {"rack.version"=>[1, 2], "rack.input"=>#<StringIO:0x007f881afc8fb0>, ... }
> app.controller.class
=> UsersController
POST リクエストの場合
POST リクエストを発行するためには、CRSF 対策に対応するために authenticate token を取得する必要がある点に注意。
# devise での認証が必要な場合、
# > u = User.find_by({name: "admin"})
# > include Warden::Test::Helpers
# > login_as u, scope: :user
> app.get '/users/new'
> require 'nokogiri'
> token = Nokogiri::HTML.parse(app.response.body).xpath('//input[@name="authenticity_token"]').attribute('value').value
> app.post '/users', { user: { name: 'tom' }, authenticity_token: token }
Started POST "/users" for 127.0.0.1 at 2014-04-08 16:29:04 +0900
Processing by UsersController#create as HTML
Parameters: {"user"=>{"name"=>"tom"}, "authenticity_token"=>"54XB85ieDmTKxSNZDggnIf1grq7/T8XiproSXTphYQ8="}
(10.4ms) BEGIN
User Exists (4.4ms) SELECT 1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT 1
(4.9ms) ROLLBACK
Rendered users/new.html.erb within layouts/application (4.2ms)
Rendered layouts/_shim.html.erb (0.1ms)
Rendered layouts/_header.html.erb (0.4ms)
Rendered layouts/_footer.html.erb (0.2ms)
Completed 200 OK in 68ms (Views: 19.7ms | ActiveRecord: 19.8ms)
=> 200
> app.controller.params
=> {"user"=>{"name"=>"hoge"},
"authenticity_token"=>"UhlDVaeqFeTphJ7WyHv6je1Gu/JX1VbGu9U/pOOIvL4=",
"action"=>"create",
"controller"=>"users"}
> app.controller.instance_variable_get(:@user).errors.full_messages
=> ["Email can't be blank", "Email is invalid"]