ruby-on-rails-3 – 宝石测试使用url_for找不到路由
我认为我的宝石测试要运行的虚拟应用程序没有正确设置,因为当我在gem的助手中调用Gadget实例(来自虚拟应用程序的存根模型)上的url_for时,我得到
undefined method `gadgets_path' for #<#<Class:0x007fe274bc1228>:0x007fe273d45eb0> 背景:我分叉了一个宝石并做了一些重大改变. (Here’s the fork.)现在我正在尝试使rspec测试工作,以便我可以验证我的更新. 测试设置类似于Rails引擎,在spec目录中有一个虚拟应用程序.该应用程序有一个模型(小工具),具有适当的控制器和spec / dummy / environment / routes.rb文件中声明的资源: Dummy::Application.routes.draw do resources :gadgets end spec / spec_helper.rb文件如下所示: ENV["RAILS_ENV"] ||= "test" require File.expand_path("../dummy/config/environment",__FILE__) require 'rspec/rails' require 'rspec/autorun' RSpec.configure do |config| config.mock_framework = :rspec config.fixture_path = "#{::Rails.root}/spec/fixtures" config.use_transactional_fixtures = true config.infer_base_class_for_anonymous_controllers = false config.order = "random" config.include Rails.application.routes.url_helpers end (你实际上可以在the project’s github repo看到完整的测试设置.我实际上已经开了一个星期左右的an issue,但直到现在我才开始尝试解决它.??) 一个未挂起的测试创建一个Gadget实例,然后将该调用作为参数调用.当帮助程序尝试url_for(@gadget)时,它会触发上述错误. 这有什么不对? ETA Dec 04:更新了当前的spec_helper.rb. 解决方法
更新
把它放在你的spec_helper.rb里 – 至少这对我有用(我克隆你的回购) ActionView::TestCase::TestController.instance_eval do helper Rails.application.routes.url_helpers#,(append other helpers you need) end ActionView::TestCase::TestController.class_eval do def _routes Rails.application.routes end end 真正的问题是,在使用路径助手方法扩展ActionController :: Base之前,TestController是从ActionController :: Base子类化的. 要使用路由助手,您应该插入 Rspec.configure do |config| config.include Rails.application.routes.url_helpers ... end 在你的spec_helper.rb中,它使所有的something_path方法可用. helper.stub!(:url_for).and_return("/path") (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |