简体   繁体   English

RSpec:未初始化的常量ActiveRecord(NameError)

[英]RSpec: uninitialized constant ActiveRecord (NameError)

When I run rspec from the root of my rails project, I get the following error: 当我从rails项目的根目录运行rspec ,出现以下错误:

/Users/ysername/code/fsf/app/models/school_application.rb:3:in `<top (required)>': uninitialized constant ActiveRecord (NameError)

which is being triggered by the call to require_relative in my spec_helper.rb file 这是由我的spec_helper.rb文件中的require_relative调用触发的

Here is my test: 这是我的测试:

require 'spec_helper'

describe '#to_xml' do
  it 'returns the xml-ified version of a payment' do
    expect(SchoolApplication.to_xml(XXXXXXXXXXXXXXXX,10, 400, "bob").to eq("<txn>   <ssl_merchant_id>5</ssl_merchant_id><ssl_user_id>3</ssl_user_id><ssl_test_mode>false</ssl_test_mode><ssl_card_number>2443444433334444</ssl_card_number><ssl_amount>400</ssl_amount><ssl_ssl_cvv2cvc2_indicator>1</ssl_cvv2cvc2_indicator><ssl_first_name>'bob'</ssl_first_name></txn>"))
  end
end

Here is my spec_helper.rb file. 这是我的spec_helper.rb文件。

require 'rubygems'
ENV["RAILS_ENV"] ||= 'test'
require_relative("../app/models/school_application") 

FYI when I remove the require_relative statement it no longer knows what SchoolApplication is. 仅当我删除require_relative语句时,它不再知道SchoolApplication是什么。 Not really sure what is going on here. 不太确定这里发生了什么。 I have looked at other threads but I am confused about how their solutions/issues coincide with my own (eg having another copy of Active_Record in my /lib/ dir???) 我看过其他线程,但我很困惑他们的解决方案/问题如何与我自己的一致(例如在我的/ lib / dir中有另一个Active_Record副本???)

Thanks! 谢谢!

Here is the file that is throwing the error, school_application.rb 这是抛出错误的文件, school_application.rb

require 'builder'

class SchoolApplication < ActiveRecord::Base
   def self.to_xml(number,expiration,cvv,amount, name)
     xml = ::Builder::XmlMarkup.new
     xml.txn {
       xml.ssl_merchant_id 5
       xml.ssl_user_id 3
       xml.ssl_ssl_pin 1434
       xml.ssl_test_mode false  
       xml.ssl_card_number number
       xml.ssl_amount amount
       xml.ssl_cvv2cvc2_indicator cvv
       xml.ssl_first_name name
     }
     xml
   end
end

PS don't worry all of the payment credentials are fabricated. PS不担心所有的付款凭证都是伪造的。

Your spec_helper is not loading the Rails environment. 您的spec_helper未加载Rails环境。 Try requiring the environment: 尝试要求环境:

require 'rubygems'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)

When setup correctly, your models are loaded automatically, so you can remove the require_relative line. 正确设置后,模型会自动加载,因此您可以删除require_relative行。

You can also generate a new spec_helper.rb if you have the rspec-rails gem installed: 如果安装了rspec-rails gem,也可以生成新的spec_helper.rb:

rails generate rspec:install

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NameError:未初始化的常量 - activerecord - NameError: uninitialized constant — activerecord NameError:RSpec上的未初始化常量 - NameError: uninitialized constant on RSpec <main> &#39;:未初始化的常量ActiveRecord(NameError) - <main>': uninitialized constant ActiveRecord (NameError) NameError:未初始化的常量CreatesPointOfInterest Rspec - NameError: uninitialized constant CreatesPointOfInterest Rspec RSpec:NameError:未初始化的常量<MyControllerName> - RSpec: NameError: uninitialized constant <MyControllerName> TextMate中的RSpec:NameError:未初始化的常量RSpec - RSpec in TextMate: NameError: uninitialized constant RSpec Sidekiq安装-未初始化的常数ActiveRecord :: Base(NameError) - Sidekiq Install - uninitialized constant ActiveRecord::Base (NameError) “ <module:ActiveRecord> &#39;:未初始化的常量CarrierWave :: Mount(NameError) - '<module:ActiveRecord>': uninitialized constant CarrierWave::Mount (NameError) NameError:未初始化的常量ActiveRecord :: ConnectionAdapters :: PostgreSQLAdapter :: TableDefinition - NameError: uninitialized constant ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition Rails:NameError(未初始化的常量 ActiveRecord::RecordNotUnique) - Rails: NameError (uninitialized constant ActiveRecord::RecordNotUnique)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM