uninitialized constant ApplicationRecord rspec - ruby-on-rails

1) created a model called Skill
2) ran some seeds
3) ran rspec --init
4) created file skill_spec.rb with the code below
require_relative "../app/models/skill"
describe Skill do
describe "database" do
it "should have 42 skills" do
expect(Skill.all.count).to eq(42)
end
end
end
5) when I run rspec in console get error:
Failure/Error: class Skill < ApplicationRecordNameError:
uninitialized constant ApplicationRecord
I already have a file application_record.rb with the following code
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end

For rails specs use require 'rails-helper' at beginning of each spec file (it is generated by bin/rails generate rspec:install from rspec-rails gem)
It contains line require File.expand_path('../config/environment', __dir__) that will load your rails environment and you'll have autoloading and all other rails parts working.

Related

Loading namespaced modules in Minitest test files in rails

Rails 5.2.1 with minitest 5.1
I have a file app/strategies/foo/bar/baz.rb that I want to namespace
baz.rb:
module Foo
module Bar
class Baz
in rails console I can successfully run:
test = Foo::Bar::Baz.new
in testing /test/strategies/foor/bar/baz_test.rb:
require 'test_helper'
class BazTest < ActiveSupport::TestCase
test 'I can call it' do
test = Foo::Bar::Baz.new
end
and I get:
NameError: uninitialized constant BazTest::Foo
I've tried varies forms of require at the top of baz_test.rb
require '/app/strategies/foo/bar/baz'
require '../../../../app/strategies/foo/bar/baz'
require 'baz'
All with the same error *** LoadError Exception: cannot load such file
Any suggestions would be appreciated!
Figured it out!
/test/strategies/foo/bar/baz_test.rb class definition needed to be:
class Foo::Bar::BazTest < ActiveSupport::TestCase

Fixture Class Not found error on simple rails testing

I am trying to set up the rails testing framework but am facing some issues. My setup is as follows
test/models/clinic_test.rb
require 'test_helper'
class ClinicTest < ActiveSupport::TestCase
test "sample" do
clinic = clinics(:myclinic)
assert(clinic.name == 'Krishan')
end
end
test/fixtures/clinics.yml
myclinic:
name: Krishan
But when I run the clinic_test rake process I get the following error:
ActiveRecord::FixtureClassNotFound: No class attached to find
test/models/clinic_test.rb:5:in `block in <class:ClinicTest>'
I see that the database is actually populated with the sample data from the clinics.yml file.
Where is the problem? Is this some configuration issue?
Add following lines in test_helper.rb file, before fixtures :all
class ActiveSupport::TestCase
self.use_transactional_fixtures = true
set_fixture_class clinics: Clinic
fixtures :all
...
end
clinics is the name of yml file where Clinic is the name of model.

uninitialized constant ActiveSupport::TestCase

When I try to run the following code:
test/models/tweet_test.rb
require 'minitest/spec'
require 'minitest/autorun'
class TweetTest < ActiveSupport::TestCase
test "Tweet should not be null" do
tweet = true
assert tweet
end
end
I receive this error:
tweet_test.rb:4:in `<main>': uninitialized constant ActiveSupport (NameError)
I am following along with a tutorial perfectly. Why is this happening? Has ActiveSuport::TestCase been deprecated?
UPDATE:
I attempted to require 'test_helper' :
require 'minitest/spec'
require 'minitest/autorun'
require 'test_helper'
class TweetTest < ActiveSupport::TestCase
test "Tweet should not be null" do
tweet = true
assert tweet
end
end
But received the following error:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- test_helper (LoadError)
You are not requiring the correct Rails minitest files to setup an ActiveSupport::TestCase. That is a Rails specific class that cannot be found in minitest alone. Are you sure you are working inside a Rails project?
In most cases, you setup a test in Rails by requiring test_helper in your test class so you can inherit from ActiveSupport::TestCase. The file test_helper contains all the requirements and setup for running tests in a Rails project.
Quick fix: just inherit from Minitest::Test.

Rails not including modules on Models when run in Rake

In Rails 3.2.16, I have a model:
class Person < ActiveRecord::Base
module Testmodule
def testmethod
puts "It's included"
end
end
include Testmodule
end
when I run bundle exec rails c, I can type Person.new.testmethod and get the expected result
If I create a small rake task:
task :myraketask => :environment do
Person.new.testmethod
end
I receive an error that testmethod isn't defined on Person.
However, if I explicitely set eager loading in the rake task, it will work:
task :myraketask => :environment do
Rails.application.eager_load!
Person.new.testmethod
end
If I create a brand new Rails project, I cannot replicate the error. Can anybody point out to me what may be wrong in my project that is causing the error in the first rake task?

pry on rspec in cloud 9 ide

I am new to RoR. I have two problems here.
ISSUE 1
my rspec is
require 'spec_helper'
describe RefUserCategory do
describe "validations" do
it { should validate_presence_of(:user_category_description) }
end
end
my Ref_User_Category model is
class RefUserCategory < ActiveRecord::Base
validate :user_category_description , presence: true
has_many :Users
end
The error I got in Rspec is
Expected errors to include /can't be blank/ when user_category_description is set to nil, got no errors
So I decided to to use Pry to check whats happening
ISSUE 2
Im my application folder I do
pry -r ./config/environment
[2] pry(main)> Ref_User_Category.new
LoadError: Unable to autoload constant Ref_User_Category, expected /var/lib/stickshift/52d29d0e5004461024000031/app-root/data/736003/east/app/models/ref_user_category.rb to define it
from /var/lib/stickshift/52d29d0e5004461024000031/app-root/data/lib/ruby/gems/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:464:in `load_missing_constant'
I have user model too I
pry(main)> User.new
gives error
ActiveRecord::StatementInvalid: Could not find table 'users'
/app-root/data/lib/ruby/gems/gems/activerecord-4.0.2/lib/active_record/connection_adapters/sqlite3_adapter.rb:512:in `table_structure'
Regarding issue 1, you have a typo: change validate to validates.
Regarding issue 2:
You generally start the console from the root of your project like this:
$ rails c
You are doing Ref_User_Category.new when you should do RefUserCategory.new.
About User.new, it looks like you haven't run your database migrations:
$ rake db:migrate
$ rake db:test:prepare

Resources