我正在将 Rspec 测试添加到以前没有 rspec 或根本没有自动化测试的项目中。它在 Windows 7 机器上运行。当我运行 rspec 时,它显示错误:
undefined method `has_attached_file'
主要问题是我正在为一个根本不使用回形针的模型编写一个简单的测试。回形针用于另一个模型。我进行了广泛的研究,试图将 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
关于我应该怎么做才能使其正常工作的任何想法?
编辑似乎问题出现在加载 spec_helper.rb 之前如果我对此文件进行更改,例如故意添加错误,则没有任何反应,并且上述问题一直在发生。我必须运行测试的唯一方法是在我的模型上注释 has_attached_file 行并将 group 选项添加到我的 Gemfile 上的回形针 gem,以避免在测试环境中使用它:
gem "paperclip", :git => 'git://github.com/thoughtbot/paperclip.git', :group=>[:development,:production]
不知道如何在不从测试环境中跳过 Paperclip 的情况下解决这个问题。