[英]ActionController::RoutingError: No route matches error while unit testing
我找到了很多同名的问题,但找不到合适的答案。
我正在测试一个控制器
球员控制器
def create
player = Player.create(params[':player'])
if player.valid?
# if creation successful, log the player in:
player_session = PlayerSession.create(
player: player,
session_token: ActiveSupport::SecureRandom.urlsafe_base64
)
render json: {session_token: player_session.session_token}
else
render json: {error: "Player name already exists."}, status: :unprocessable_entity
end
end
玩家控制器测试
test "create" do
post(:create,
{
'player' => {
'player_name' => "usman",
'password' => 123,
'email' => 'ranasaani@gmail.com'
}
}
)
assert_select response.body
end
执行测试文件时,控制台上会显示以下错误 。
ActionController::RoutingError: No route matches {:player=>{"player_name"=>"usman", "password"=>123, "email"=>"ranasaani@gmail.com"}, :controller=>"pl
ayers", :action=>"create"}
D:/jruby-1.7.1/lib/ruby/gems/shared/gems/actionpack-3.0.3/lib/action_dispatch/routing/route_set.rb:424:in `raise_routing_error'
D:/jruby-1.7.1/lib/ruby/gems/shared/gems/actionpack-3.0.3/lib/action_dispatch/routing/route_set.rb:406:in `generate'
D:/jruby-1.7.1/lib/ruby/gems/shared/gems/actionpack-3.0.3/lib/action_dispatch/routing/route_set.rb:453:in `generate'
D:/jruby-1.7.1/lib/ruby/gems/shared/gems/actionpack-3.0.3/lib/action_dispatch/routing/route_set.rb:449:in `generate_extras'
D:/jruby-1.7.1/lib/ruby/gems/shared/gems/actionpack-3.0.3/lib/action_dispatch/routing/route_set.rb:445:in `extra_keys'
D:/jruby-1.7.1/lib/ruby/gems/shared/gems/actionpack-3.0.3/lib/action_controller/test_case.rb:143:in `assign_parameters'
D:/jruby-1.7.1/lib/ruby/gems/shared/gems/actionpack-3.0.3/lib/action_controller/test_case.rb:402:in `process'
D:/jruby-1.7.1/lib/ruby/gems/shared/gems/actionpack-3.0.3/lib/action_controller/test_case.rb:47:in `process'
D:/jruby-1.7.1/lib/ruby/gems/shared/gems/actionpack-3.0.3/lib/action_controller/test_case.rb:355:in `post'
D:/Projects/lyricle/test/functional/players_controller_test.rb:5:in `test_create'
org/jruby/RubyBasicObject.java:1659:in `__send__'
为什么这个错误在这里?
如果您希望任何东西能够访问您的控制器,则必须定义路径 。
所以,在你的config/routes.rb
:
App::Application.routes.draw do
resources :your_resource_plural_name
end
create动作应该是这样的:
def create
@player = Player.new(params[:player])
#your code
end
您还需要在创建操作之前添加新操作:
def new
@player = Player.new
end
在routes.rb中你应该有resources :players
@player
是一个实例变量,使您能够在视图中调用它。
请参阅Rails指南以创建您的第一个应用程序。
还检查Rails for Zombies他们非常好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.