Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec: uninitialized constant ActiveRecord (NameError)

When I run rspec from the root of my rails project, I get the following error:

/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

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.

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. 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 (e.g. having another copy of Active_Record in my /lib/ dir???)

Thanks!

Here is the file that is throwing the error, 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

P.S. don't worry all of the payment credentials are fabricated.

like image 571
Thalatta Avatar asked Jul 15 '14 17:07

Thalatta


1 Answers

Your spec_helper is not loading the Rails environment. 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.

You can also generate a new spec_helper.rb if you have the rspec-rails gem installed:

rails generate rspec:install
like image 62
infused Avatar answered Nov 15 '22 05:11

infused