我正在尝试使用factory_girl_rails在Rails 4.1.9上设置rspec(v3.0.2),但是在实现rails_helper.rb和spec_helper.rb之后,运行_spec.rb文件时出现以下错误:
/Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record/attribute_methods.rb:9:in `<module:AttributeMethods>': uninitialized constant ActiveModel::AttributeMethods (NameError)
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record/attribute_methods.rb:7:in `<module:ActiveRecord>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record/attribute_methods.rb:5:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record.rb:101:in `<module:ActiveRecord>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record.rb:31:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record/railtie.rb:1:in `require'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record/railtie.rb:1:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/railties-4.1.9/lib/rails/all.rb:12:in `require'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/railties-4.1.9/lib/rails/all.rb:12:in `block in <top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/railties-4.1.9/lib/rails/all.rb:10:in `each'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/railties-4.1.9/lib/rails/all.rb:10:in `<top (required)>'
from /Users/user/code/rtb-campaign-optimization/config/application.rb:3:in `require'
from /Users/user/code/rtb-campaign-optimization/config/application.rb:3:in `<top (required)>'
from /Users/user/code/rtb-campaign-optimization/config/environment.rb:2:in `require'
from /Users/user/code/rtb-campaign-optimization/config/environment.rb:2:in `<top (required)>'
from /Users/user/code/rtb-campaign-optimization/spec/rails_helper.rb:4:in `require'
from /Users/user/code/rtb-campaign-optimization/spec/rails_helper.rb:4:in `<top (required)>'
from /Users/user/code/rtb-campaign-optimization/spec/models/cpa_calculator_spec.rb:1:in `require'
from /Users/user/code/rtb-campaign-optimization/spec/models/cpa_calculator_spec.rb:1:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `load'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `block in load_spec_files'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `each'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `load_spec_files'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:97:in `setup'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:85:in `run'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:70:in `run'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:38:in `invoke'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/exe/rspec:4:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `load'
from /Users/user/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `<main>'
from /Users/user/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
from /Users/user/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'
这是我的gemfile:
group :development do
gem 'rails-dev-tweaks'
gem 'quiet_assets'
gem 'pry'
gem 'pry-byebug'
gem 'hirb'
gem 'spring'
gem 'spring-commands-rspec'
end
group :development, :test do
gem 'rspec-rails', '~> 3.0.0'
gem 'factory_girl_rails'
end
group :test do
gem 'database_cleaner'
end
rails_helper.rb:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
end
spec_helper.rb:
RSpec.configure do |config|
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
config.filter_run :focus
config.run_all_when_everything_filtered = true
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
expectations.syntax = :expect
end
# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
mocks.syntax = :expect
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended.
mocks.verify_partial_doubles = true
end
end
我的_spec文件:
require 'rails_helper'
describe 'CampaignsManagement::CpaCalculator' do
let(:campaign) {
camp = FactoryGirl.build_stubbed(:campaign)
# campaign_histories_per_campaigns.times.each do |index|
# Factory.build(:campaign_history, value: index + 1, campaign_id: camp.id)
# end
camp
}
context "when there are some active campaigns" do
context "when the campaign have histories" do
let(:campaign_histories_per_campaigns) { 10 }
context 'when the campaign have histories created today' do
end
context 'when the campaign has only histories in the past (not generated today)' do
end
end
context "when the campaign is histories orphaned" do
let(:campaign_histories_per_campaigns) { 0 }
it "should not update redshift" do
campaign.campaign_histories.size.should.eq(0)
end
end
end
end
application.rb中:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module RtbCampaignOptimization
class Application < Rails::Application
config.assets.enabled = false
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.autoload_paths << Rails.root.join('lib')
end
end
任何想法?
由于
答案 0 :(得分:0)
尝试运行:
rails generate rspec:install //which you must have already run
bundle install
还有check out this question and it's answers
根据以上所述,您需要将spork-rails添加到您的Gemfile中。
所有以上共享的代码片段看起来都很好看。