ruby-on-rails – Rspec路由规范失败,param id被反转?
发布时间:2020-12-17 03:48:24  所属栏目:百科  来源:网络整理 
            导读:我的rspec路由规范给出了不清楚的错误.在预期的params v / s中,id param处于反向位置的真实参数.为什么以及如何解决? require "spec_helper"describe GameController do describe "routing" do game = FactoryGirl.create(:game) it "routes to #show" do g
                
                
                
            | 
 我的rspec路由规范给出了不清楚的错误.在预期的params v / s中,id param处于反向位置的真实参数.为什么以及如何解决? 
  
  
  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,"action"=>"show"}>,difference:.
       <{"id"=>1,"action"=>"show"}> expected but was
       <{"action"=>"show","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")(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
