加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – RSpec找不到我的命名路线

发布时间:2020-12-17 03:31:43 所属栏目:百科 来源:网络整理
导读:我有一个莫名其妙的困难时间让我的命名路线在我的rspec测试中工作. 这就是我所看到的: 1) Home GET / works! Failure/Error: get root_path NameError: undefined local variable or method `root_path' for #RSpec::Core::ExampleGroup::Nested_1::Nested_
我有一个莫名其妙的困难时间让我的命名路线在我的rspec测试中工作.

这就是我所看到的:

1) Home GET / works!
 Failure/Error: get root_path
 NameError:
   undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fe13b0c1538>
 # ./spec/requests/home_spec.rb:6:in `block (3 levels) in <top (required)>'

2) Home GET / shows products!
 Failure/Error: get products_path
 NameError:
   undefined local variable or method `products_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fe13b0cdea0>
 # ./spec/requests/home_spec.rb:10:in `block (3 levels) in <top (required)>'

规格/请求/ home_spec.rb

require 'spec_helper'

describe "Home" do
  describe "GET /" do
    it "works!" do
      get root_path
      response.status.should be(200)
    end
    it "shows products!" do
      get products_path
      response.status.should be(200)
    end
  end  
end

投机/ spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment",__FILE__)
require 'rspec/rails'
require 'rspec/autorun'

# Requires supporting ruby files with custom matchers and macros,etc,# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

require 'spree/core/testing_support/factories'

RSpec.configure do |config|
  config.mock_with :rspec
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
  config.before(:suite) do
    # DatabaseCleaner.strategy = :truncation
  end
  config.before(:each) do
    # DatabaseCleaner.start
  end
  config.after(:each) do
    # DatabaseCleaner.clean
  end

  config.include Rails.application.routes.url_helpers
end

您可以看到测试包含spec_helper,spec_helper包含路由方法.这是我的佣金路线:

捆绑exec rake路线

...
             root        /                        spree/home#index
         products GET    /products(.:format)      spree/products#index
...

知道这可能会发生什么,以防止路线工作?谢谢!

解决方法

如果您想要从虚拟应用程序中的测试或仅仅是应用程序本身引用引擎的路由,那么您需要在路由助手前面添加引擎名称.对于Spree,您可以参考以下路线:

spree.products_path

同样,如果要从引擎引用主应用程序的路由,则需要使用main_app:

main_app.root_path

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读