Rails rspec LoadError:无法加载此类文件 - main_helper

时间:2016-09-22 18:00:33

标签: ruby-on-rails rspec

我在spec/features/authorization_spec.rb

中有这段代码
let!(:user) { FactoryGirl.create(:user) }
let!(:q) { FactoryGirl.create(:question, user: user, body: "sample text", title: "sample title") }
let!(:answer) { FactoryGirl.create(:answer, user: user, question: q) }

我想把它转移到帮手。 所以我在helpers文件夹

中创建了main_helper.rb
RSpec.describe MainHelper, type: :helper do
    def auth
      let!(:user) { FactoryGirl.create(:user) }
      let!(:q) { FactoryGirl.create(:question, user: user, body: "sample text", title: "sample title") }
      let!(:answer) { FactoryGirl.create(:answer, user: user, question: q) }
    end
end

在我的authorization_spec.rb中 我写了这个:

require 'main_helper'

RSpec.describe "Authorization", type: :request do
MainHelper.auth

但得到了一个错误: LoadError: cannot load such file -- main_helper 我删除了所需的行并得到了这个错误: UPD: $ load path:

irb(main):003:0> $LOAD_PATH
=> ["/home/direwolf/rails_projects/test_v1/lib", "/home/direwolf/rails_projects/test_v1/vendor", "/home/direwolf/rails_projects/test_v1/app/assets", "/home/direwolf/rails_projects/test_v1/app/controllers", "/home/direwolf/rails_projects/test_v1/app/helpers", "/home/direwolf/rails_projects/test_v1/app/mailers", "/home/direwolf/rails_projects/test_v1/app/models", "/home/direwolf/rails_projects/test_v1/app/controllers/concerns", "/home/direwolf/rails_projects/test_v1/app/models/concerns", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/devise-4.2.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/devise-4.2.0/app/controllers", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/devise-4.2.0/app/helpers", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/devise-4.2.0/app/mailers", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/jquery-turbolinks-2.1.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/jquery-turbolinks-2.1.0/vendor", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/turbolinks-5.0.1/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/jquery-rails-4.2.1/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/jquery-rails-4.2.1/vendor", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/coffee-rails-4.1.1/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.1/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib", "/home/direwolf/.rbenv/rbenv.d/exec/gem-rehash", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/uglifier-3.0.2/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spork-rails-4.0.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/shoulda-matchers-2.8.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/selenium-webdriver-2.53.4/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/websocket-1.2.3/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sdoc-0.4.1/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sass-rails-5.0.6/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/tilt-2.0.5/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sass-3.4.22/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rubyzip-1.2.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-rails-3.5.2/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rdoc-4.2.2/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rails-4.2.6/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sprockets-rails-3.2.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/sprockets-3.7.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pg-0.18.4/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86-linux/2.3.0-static/pg-0.18.4", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/libnotify-0.9.1/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/turbolinks-source-5.0.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/jbuilder-2.6.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/multi_json-1.12.1/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/guard-spork-2.1.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spork-1.0.0rc4/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/guard-rspec-4.7.3/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-3.5.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-mocks-3.5.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-expectations-3.5.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.3/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-support-3.5.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/guard-compat-1.2.1/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/guard-2.14.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/slop-3.6.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/method_source-0.8.2/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/notiffany-0.1.1/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/shellany-0.0.1/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/nenv-0.3.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/lumberjack-1.0.10/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/listen-3.1.5/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/ruby_dep-1.4.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rb-inotify-0.9.7/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rb-fsevent-0.9.7/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/formatador-0.2.5/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/factory_girl_rails-4.7.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/factory_girl-4.7.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/differ-0.1.2/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/diff-lcs-1.2.5/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/warden-1.2.6/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/responders-2.3.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/orm_adapter-0.5.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/concurrent-ruby-1.0.2/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-4.2.6/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thor-0.19.1/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/coffee-script-2.4.1/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/execjs-2.7.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/coffee-script-source-1.10.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/coderay-1.1.1/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/childprocess-0.5.9/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/ffi-1.9.14/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86-linux/2.3.0-static/ffi-1.9.14", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/capybara-2.8.1/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/xpath-2.0.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/byebug-9.0.5/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86-linux/2.3.0-static/byebug-9.0.5", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/binding_of_caller-0.7.2/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86-linux/2.3.0-static/binding_of_caller-0.7.2", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/debug_inspector-0.0.2/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86-linux/2.3.0-static/debug_inspector-0.0.2", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bcrypt-3.1.11/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86-linux/2.3.0-static/bcrypt-3.1.11", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/addressable-2.4.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/arel-6.0.3/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activemodel-4.2.6/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionmailer-4.2.6/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mail-2.6.4/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mime-types-3.1/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mime-types-data-3.2016.0521/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activejob-4.2.6/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/globalid-0.3.7/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-test-0.6.3/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-1.6.4/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionview-4.2.6/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rails-html-sanitizer-1.0.3/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/loofah-2.0.3/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rails-dom-testing-1.0.7/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rails-deprecated_sanitizer-1.0.3/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/nokogiri-1.6.8/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86-linux/2.3.0-static/nokogiri-1.6.8", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pkg-config-1.1.7/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mini_portile2-2.1.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/erubis-2.7.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/builder-3.2.2/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/tzinfo-1.2.2/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thread_safe-0.3.5/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/minitest-5.9.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/json-1.8.3/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/i18n-0.7.0/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-11.2.2/lib", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/site_ruby/2.3.0", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/site_ruby/2.3.0/i686-linux", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/site_ruby", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/vendor_ruby/2.3.0", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/vendor_ruby/2.3.0/i686-linux", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/vendor_ruby", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/2.3.0", "/home/direwolf/.rbenv/versions/2.3.1/lib/ruby/2.3.0/i686-linux"]

1 个答案:

答案 0 :(得分:0)

spec/helpers是RSpec rails为rails helpers设置规范的地方。

$ rails generate helper foo
Running via Spring preloader in process 19017
      create  app/helpers/foo_helper.rb
      invoke  rspec
      create    spec/helpers/foo_helper_spec.rb

将文件移至spec/support,这是任何仅由测试系统使用的文件应该去的地方。

$ mv spec/helpers/main_helper.rb spec/support/main_helper.rb 

RSpec将/spec目录添加到加载路径。这就是你可以做require 'spec_helper'的原因。要从支持目录中获取文件,您可以require 'support/main_helper'