gpt4 book ai didi

ruby-on-rails - Rspec 路由规范在参数 id 反转的情况下导致失败?

转载 作者:行者123 更新时间:2023-12-04 23:02:48 25 4
gpt4 key购买 nike

我的 rspec 路由规范给出了不清楚的错误。在预期参数 v/s 实际参数中,id 参数处于相反的位置。为什么以及如何解决?

require "spec_helper"

describe GameController do
describe "routing" do

game = FactoryGirl.create(:game)

it "routes to #show" do
get("/game/1").should route_to("game#show", :id => 1)
end

end
end

这会引发错误:
  1) gameController routing routes to #show
Failure/Error: get("/game/1").should route_to("game#show", :id => 1)
The recognized options <{"action"=>"show", "controller"=>"game", "id"=>"1"}> did not match <{"id"=>1, "controller"=>"game", "action"=>"show"}>, difference:.
<{"id"=>1, "controller"=>"game", "action"=>"show"}> expected but was
<{"action"=>"show", "controller"=>"game", "id"=>"1"}>.
# ./spec/routing/game_routing_spec.rb:11:in `block (3 levels) in <top (required)>'

最佳答案

Rails 将参数解析为字符串,而不是整数,因此 params[:id]真的被分配了"1"而不是 1 .

尝试期待一个字符串:

get("/game/1").should route_to("game#show", :id => "1")

关于ruby-on-rails - Rspec 路由规范在参数 id 反转的情况下导致失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19230884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com