ruby-on-rails - rspec 测试中所有 get/put 调用错误 w/"no route matches";尽管它们在应用程序中运行良好

标签 ruby-on-rails ruby-on-rails-3 rspec routing routes

在我的应用程序中工作正常的路由在 rspec 测试中的任何 get/put 调用都失败并显示“无路由匹配”。我在这里做错了什么?

这是一个简单的例子,来自 contracts_controller_spec.rb:

it 'should redirect to edit on show' do
  get :show
  response.should be_success
  response.should render_template(:edit)
end

以上失败并出现以下情况:

ContractsController api calls should redirect to edit on show
  Failure/Error: get :show
  ActionController::RoutingError:
    No route matches {:controller=>"contracts", :action=>"show"}

contracts_controller.rb 中的show 方法:

def show
  Rails.logger.debug("getting contract info....")
  get_contract_info
  Rails.logger.debug("...got contract info.")
  render :action => :edit
end

routes.rb 内容:

resource :contract, :only=>[:show, :edit, :update], :protocol =>ROUTES_PROTOCOL do
  member do
    get :print
  end
end

rake routes 输出:

print_contract GET  /contract/print(.:format)    contracts#print {:protocol=>"http"}
 edit_contract GET  /contract/edit(.:format)     contracts#edit {:protocol=>"http"}
      contract GET  /contract(.:format)          contracts#show {:protocol=>"http"}
               PUT  /contract(.:format)          contracts#update {:protocol=>"http"}
  • 使用 rspec-rails 2.14.0
  • 此应用最近从 Rails 2.3 升级到 3.2,在其他方面取得了成功
  • 注意非标准的显示/编辑路由:不需要 ID,强制使用 ID 仍然会导致 No route matches {:controller=>"contracts", :id=>"1", :action =>“显示”

最佳答案

:protocol 有点奇怪,尝试删除 ??

关于ruby-on-rails - rspec 测试中所有 get/put 调用错误 w/"no route matches";尽管它们在应用程序中运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18495637/

相关文章:

ruby-on-rails-3 - 如何使用 Devise 和 rspec 测试 Rails3 引擎

ruby-on-rails - rails -find_by_sql : wrong number of arguments (2 for 1)

ruby-on-rails - 如何从我的 Rails Web 应用程序更新用户的 Facebook 个人资料状态

ruby-on-rails - 如何使用send_file下载文件?

ruby-on-rails - 无法使用 Authlogic on Rails 3.0.1 设置 current_user

ruby-on-rails - rails "undefined method"从布局 View 调用辅助方法时

ruby-on-rails - Rails 的 Turbolinks 未激活

ruby - 如何清除 ruby​​ 中 rspec 测试之间的类变量

ruby-on-rails - OAuth::问题(参数不存在)

ruby-on-rails - 在 rails 中测试的正确方法