我将Rspec测试添加到之前没有rspec的项目中,或者根本就是自动化测试。它在Windows 7机器上运行。当我运行rspec时,它显示错误:
undefined method `has_attached_file'
主要问题是我只为一个完全不使用Paperclip的模型编写了一个简单的测试。 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
我应该怎样做才能使其正常运作?
编辑在加载spec_helper.rb之前似乎出现了问题 如果我对此文件进行更改,例如故意添加错误,则不会发生任何事情,并且上述问题仍在继续。 我必须运行测试的唯一方法是在我的模型上注释has_attached_file行,并将group选项添加到我的Gemfile上的paperclip gem,以避免在测试环境中使用它:
gem "paperclip", :git => 'git://github.com/thoughtbot/paperclip.git', :group=>[:development,:production]
如果不从测试环境中跳过Paperclip,不知道如何解决这个问题。
答案 0 :(得分:4)
在我看来问题是与shoulda-matchers和paperclip一起使用
添加到spec_helper.rb文件
require "paperclip/matchers"
下面你在哪里
require "capybara/rspec"
并添加
config.include Paperclip::Shoulda::Matchers
以下
RSpec.configure do |config|
查看shoulda-matchers documentation for paperclip
希望这有帮助
答案 1 :(得分:0)
我在尝试在具有ActiveRecord而不是Rails的gem中使用Paperclip时遇到了这个问题。我能够通过添加img.crossOrigin = "Anonymous";
文件来解决这个问题:
spec/support/paperclip.rb
同样建议,这是添加require 'paperclip/railtie'
RSpec.configure do |config|
# Bind paperclip to enable ActiveRecord #has_attached_file method
Paperclip::Railtie.insert
end
的好地方。
但是我使用Rails 4.2和Rspec 3,所以我认为加载的东西的顺序与你的不同。