【问题标题】:Issue with Paperclip and Rspec: undefined method `has_attached_file'Paperclip 和 Rspec 的问题:未定义的方法“has_attached_file”
【发布时间】:2013-10-31 14:31:01
【问题描述】:

我正在将 Rspec 测试添加到以前没有 rspec 或根本没有自动化测试的项目中。它在 Windows 7 机器上运行。当我运行 rspec 时,它显示错误:

undefined method `has_attached_file'

主要问题是我正在为一个根本不使用 Paperclip 的模型编写一个简单的测试。回形针用于另一个模型。 我进行了广泛的研究,试图将 gem 包含在测试环境中,但似乎没有任何解决问题的方法。

当我运行 rspec 测试时,有没有办法跳过或强制包含回形针?

我的 gemfile 包含以下内容:

source 'http://rubygems.org'
ruby "1.9.2"
gem 'rails', '3.0.20'
...
gem "paperclip", :git => 'git://github.com/thoughtbot/paperclip.git'
gem 'aws-s3'
gem 'aws-sdk'
group :test, :development do
  gem 'rspec-rails', '~> 2.14.0'
end
group :test do
  gem 'sqlite3'
  gem 'capybara', '2.0'
  gem 'selenium-webdriver'
  gem 'shoulda-matchers'
  gem 'database_cleaner'
  gem 'launchy', '2.2.0'
  gem 'factory_girl_rails'
end

我运行 rspec 命令:

bundle exec rake spec

我收到以下错误:

    C:/Users/MAC/.pik/rubies/Ruby-192-p290/bin/ruby.exe -S rspec ./spec/models/filter_category_spec.rb ./spec/models/filter_spec.rb
C:/Users/MAC/.pik/rubies/Ruby-192-p290/lib/ruby/gems/1.9.1/gems/attr_encrypted-1.2.1/lib/attr_encrypted.rb:241:in `method_missing': undefined method `has_a
ttached_file' for #<Class:0x5278770> (NoMethodError)
        from C:/Users/MAC/.pik/rubies/Ruby-192-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.0.20/lib/active_record/base.rb:1018:in `method_missing'
        from C:/Users/MAC/.pik/rubies/Ruby-192-p290/lib/ruby/gems/1.9.1/gems/attr_encrypted-1.2.1/lib/attr_encrypted/adapters/active_record.rb:50:in `metho
d_missing_with_attr_encrypted'
....
       from C:/Users/MAC/.pik/rubies/Ruby-192-p290/lib/ruby/gems/1.9.1/gems/railties-3.0.20/lib/rails/application.rb:77:in `method_missing'
       from C:/Avity/DS Candidates Tracking System/ds-cts/config/environment.rb:5:in `<top (required)>'
       from C:/Avity/DS Candidates Tracking System/ds-cts/spec/spec_helper.rb:3:in `require'
       from C:/Avity/DS Candidates Tracking System/ds-cts/spec/spec_helper.rb:3:in `<top (required)>'
       from C:/Avity/DS Candidates Tracking System/ds-cts/spec/models/filter_category_spec.rb:1:in `require'

我的 spec_helper 文件是:

# 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'
require 'capybara/rspec'

# 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}

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.use_transactional_fixtures = false
  config.infer_base_class_for_anonymous_controllers = false
  config.order = "random"

  #Database cleaner
  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

  #Use chrome driver
  Capybara.register_driver :selenium do |app|
      Capybara::Selenium::Driver.new(app, :browser => :chrome)
    end
    Capybara.current_driver = :selenium_chrome
end

我应该怎么做才能使其正常工作有什么想法吗?

EDIT 问题似乎出现在加载 spec_helper.rb 之前 如果我对此文件进行更改,例如故意添加错误,则不会发生任何事情,并且上述问题会继续发生。 我必须运行测试的唯一方法是在我的模型上注释 has_attached_file 行并将 group 选项添加到我的 Gemfile 上的回形针 gem,以避免在测试环境中使用它:

gem "paperclip", :git => 'git://github.com/thoughtbot/paperclip.git', :group=>[:development,:production]

不知道如何在不从测试环境中跳过 Paperclip 的情况下解决这个问题。

【问题讨论】:

    标签: ruby-on-rails rspec paperclip


    【解决方案1】:

    在我看来问题出在 shoulda-matchers 和回形针

    添加到您的 spec_helper.rb 文件中

    require "paperclip/matchers"
    

    低于你所在的位置

    require "capybara/rspec"
    

    并添加

    config.include Paperclip::Shoulda::Matchers
    

    下面

    RSpec.configure do |config|    
    

    查看shoulda-matchers documentation for paperclip

    希望对你有帮助

    【讨论】:

    • 感谢您的快速回答。我过去尝试过,但不幸的是它没有解决问题。
    【解决方案2】:

    我尝试在具有 ActiveRecord 但没有 Rails 的 gem 中使用 Paperclip 时遇到了这个问题。我可以通过添加具有以下内容的spec/support/paperclip.rb 文件来解决此问题:

    require 'paperclip/railtie'
    
    RSpec.configure do |config|
      # Bind paperclip to enable ActiveRecord #has_attached_file method
      Paperclip::Railtie.insert
    end
    

    同样建议,这是添加config.include Paperclip::Shoulda::Matchers 的好地方。

    但我使用的是 Rails 4.2 和 Rspec 3,所以我认为加载的顺序与你的大不相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多