wry
2012-11-13, 10:45:28
Hallo,
ich spiele grad mit Ruby on Rails und bin neu auf dem Gebiet. Derzeit hab ich ein Problem mit den Tests. Ich versuche eine Controller Methode zu testen, die über eine spezielle Route angesprochen wird:
routes.rb: ( user id in der url bsp: "/api/v1/users/1/bookings" )
POST /api/v1/users/:user_id/bookings(.:format) api/v1/bookings#create
/controllers/api/v1/booking_controller.rb
# encoding: utf-8
class Api::V1::BookingsController < ApplicationController
respond_to :json
before_filter :authenticate_user!
before_filter :get_user
def create
...
end
/test/functional/api/v1/bookings_controller_test.rb
require 'test_helper'
class Api::V1::BookingsControllerTest < ActionController::TestCase
include Devise::TestHelpers
setup do
@booking = bookings(:one)
@user = users(:one)
sign_in @user
end
test "should return a bad request" do
post :create, :booking => { ... }, :format => 'json'
assert_response :bad_request
end
end
Wenn ich den Test ausführe:
ruby -I test test/functional/api/v1/bookings_controller_test.rb
bekomme ich die Fehlermeldung, dass keine Route matched. Irgendwie muss ich dem post doch den Pfad mitteilen (mit der User-id), aber ich kann beim besten Willen nichts dazu finden. Ich glaube mittlerweile, dass ich an die Sache falsch herangehe. Kennt sich vielleicht einer mit Rails etwas aus und sieht den Fehler?
Hier noch die Fehlermeldung:
Error:
test_should_return_a_bad_request(Api::V1::BookingsControllerTest):
ActionController::RoutingError: No route matches {:booking=>{:duration_minutes=>60, :parking_lot_id=>1, :user_id=>1, :license_plate=>"I_BREAK_FOR_RAINBOWS"}, :format=>"js
on", :controller=>"api/v1/bookings", :action=>"create"}
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:532:in `raise_routing_error'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:528:in `rescue in generate'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:520:in `generate'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:561:in `generate'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:557:in `generate_extras'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:553:in `extra_keys'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:147:in `assign_parameters'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:459:in `process'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:49:in `process'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/devise-2.1.2/lib/devise/test_helpers.rb:19:in `block in process'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/devise-2.1.2/lib/devise/test_helpers.rb:71:in `catch'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/devise-2.1.2/lib/devise/test_helpers.rb:71:in `_catch_warden'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/devise-2.1.2/lib/devise/test_helpers.rb:19:in `process'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:391:in `post'
test/functional/api/v1/bookings_controller_test.rb:20:in `block in <class:BookingsControllerTest>'
ich spiele grad mit Ruby on Rails und bin neu auf dem Gebiet. Derzeit hab ich ein Problem mit den Tests. Ich versuche eine Controller Methode zu testen, die über eine spezielle Route angesprochen wird:
routes.rb: ( user id in der url bsp: "/api/v1/users/1/bookings" )
POST /api/v1/users/:user_id/bookings(.:format) api/v1/bookings#create
/controllers/api/v1/booking_controller.rb
# encoding: utf-8
class Api::V1::BookingsController < ApplicationController
respond_to :json
before_filter :authenticate_user!
before_filter :get_user
def create
...
end
/test/functional/api/v1/bookings_controller_test.rb
require 'test_helper'
class Api::V1::BookingsControllerTest < ActionController::TestCase
include Devise::TestHelpers
setup do
@booking = bookings(:one)
@user = users(:one)
sign_in @user
end
test "should return a bad request" do
post :create, :booking => { ... }, :format => 'json'
assert_response :bad_request
end
end
Wenn ich den Test ausführe:
ruby -I test test/functional/api/v1/bookings_controller_test.rb
bekomme ich die Fehlermeldung, dass keine Route matched. Irgendwie muss ich dem post doch den Pfad mitteilen (mit der User-id), aber ich kann beim besten Willen nichts dazu finden. Ich glaube mittlerweile, dass ich an die Sache falsch herangehe. Kennt sich vielleicht einer mit Rails etwas aus und sieht den Fehler?
Hier noch die Fehlermeldung:
Error:
test_should_return_a_bad_request(Api::V1::BookingsControllerTest):
ActionController::RoutingError: No route matches {:booking=>{:duration_minutes=>60, :parking_lot_id=>1, :user_id=>1, :license_plate=>"I_BREAK_FOR_RAINBOWS"}, :format=>"js
on", :controller=>"api/v1/bookings", :action=>"create"}
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:532:in `raise_routing_error'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:528:in `rescue in generate'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:520:in `generate'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:561:in `generate'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:557:in `generate_extras'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:553:in `extra_keys'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:147:in `assign_parameters'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:459:in `process'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:49:in `process'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/devise-2.1.2/lib/devise/test_helpers.rb:19:in `block in process'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/devise-2.1.2/lib/devise/test_helpers.rb:71:in `catch'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/devise-2.1.2/lib/devise/test_helpers.rb:71:in `_catch_warden'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/devise-2.1.2/lib/devise/test_helpers.rb:19:in `process'
/Users/x/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:391:in `post'
test/functional/api/v1/bookings_controller_test.rb:20:in `block in <class:BookingsControllerTest>'