【问题标题】:Rspec routing specs give failure with param id reversed?Rspec 路由规范会导致参数 id 反转失败?
【发布时间】:2013-10-14 09:11:56
【问题描述】:

我的 rspec 路由规范返回了不清楚的错误。在预期参数与实际参数中,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)>'

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 rspec ruby-on-rails-4 specs


    【解决方案1】:

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

    尝试期待一个字符串:

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

    【讨论】:

    • 是的,小事可以忽略,但这完全解决了它!谢谢
    猜你喜欢
    • 1970-01-01
    • 2015-04-19
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 2012-11-16
    • 1970-01-01
    相关资源
    最近更新 更多