Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 未初始化的常量ActiveModel::AttributeMethods(NameError)_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

Ruby on rails 未初始化的常量ActiveModel::AttributeMethods(NameError)

Ruby on rails 未初始化的常量ActiveModel::AttributeMethods(NameError),ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我正在尝试使用factory\u girl\u Rails在Rails 4.1.9上设置rspec(v3.0.2),但在实现Rails\u helper.rb和spec\u helper.rb后,在运行我的\u spec.rb文件时出现以下错误: /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record/attribute_methods.rb:9:in `<module:AttributeMeth

我正在尝试使用factory\u girl\u Rails在Rails 4.1.9上设置rspec(v3.0.2),但在实现Rails\u helper.rb和spec\u helper.rb后,在运行我的\u 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>'
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
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_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
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
我的规格文件:

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
需要文件。展开路径('../boot',文件)
需要“rails/all”
#需要Gemfile中列出的宝石,包括任何宝石
#您仅限于:测试、开发或:生产。
Bundler.require(*Rails.groups)
模块RTBCampaginOptimization
类应用程序
rails generate rspec:install //which you must have already run

bundle install 
而且

根据上面的说明,您需要添加到您的文件中


以上所有共享代码片段看起来都不错

你能显示你的
config/application.rb
文件吗?请发布你的
cpa\u计算器\u spec.rb
file@AmitSharma这已经在问题的细节中了。有人有解决方案吗?添加gem“spork rails”“~>4.0.0”并没有解决我的问题。在安装rspec时,我也运行了rspec生成器,没有出现任何问题。