Rspec uninitialized constant ActiveRecord(NameError) - ruby-on-rails

I followed codeschool tutorial, but I encountered some troubles.
Here is zombie_spec.rb
#spec/model/zombie_spec.rb
require 'spec_helper'
require 'zombie'
describe Zombie do
it 'is invalid without a name' do
zombie = Zombie.new
zombie.should_not be_valid
end
end
zombie.rb
#spec/zombie.rb
class Zombie < ActiveRecord::Base
validates :name, presence: true
...
end
After I typed rspec spec/models/zombie_spec.rb, it throw uninitialized constant ActiveRecord (NameError)
I've put this project on github

I think the tutorial might be trying to transition from using RSpec on a plain Ruby object to using the rspec-rails gem on an ActiveRecord object. For the examples that use rspec-rails, you should have a model in the file app/models/zombie.rb. This is what the spec in spec/models/zombie_spec.rb will look for. Also, your specs will need to require rails_helper rather than spec_helper.
# app/models/zombie.rb
class Zombie < ActiveRecord::Base
validates :name, presence: true
def hungry?
true
end
end
# spec/models/zombie_spec.rb
require 'rails_helper'
describe Zombie do
it 'is invalid without a name' do
zombie = Zombie.new
zombie.should_not be_valid
end
end

Zombie is extending ActiveRecord::Base but your code can't find ActiveRecord.
To fix that you can require 'activerecord' within zombie.rb. Depending on whether or not it's installed, you may need to also gem install activerecord from your command line or, alternatively, add gem 'activerecord' to your Gemfile and run bundle install

I found an example that worked well for me:
require 'rails_helper'
RSpec.describe Auction, :type => :model do
it "is valid with valid attributes"
it "is not valid without a title"
it "is not valid without a description"
it "is not valid without a start_date"
it "is not valid without a end_date"
end
I am new to ruby, so I am not 100% sure why it needs this ":type" attribute, but it fixed my problem.
(Source)

Related

How to configure Sorbet with rspec?

I have a simple test but the describe keyword is not working in Sorbet tests.
The error I'm receiving on these methods:
Method `describe` does not exist on `T.class_of(<root>)`7003
RSpec.describe(Model) do
describe 'my test' do
before(:each) do # .before error
user = FactoryBot.create(:user)
end
it 'can fill in all fields' do # .it errors
end
end
end
I think I need to tell Sorbet some how that this is called in the context of spec_helper.rbbut I'm not sure how to do that.
I've already installed this gem rspec-sorbet and ran
spec/spec_helper.rb
require 'rspec/sorbet'
To silence the errors, I ran this:
RSpec.describe(Model) do
T.bind(self, T.untyped)
# T.bind(self, RSpec) This does not work either
end

What changed in Ruby?

The following spec passes fine in Ruby 2.1.5 but fails in 2.2.0 and I can't tell what that's all about:
# job.rb
class Job < ActiveRecord::Base
validates :link, :url => true
end
# job_spec.rb
require 'rails_helper'
describe Job do
describe "#create" do
["blah", "http://", " "].each do |bad_link|
it {
should_not allow_value(bad_link).for(:link)
}
end
end
end
fail log looks like this:
1) Job#create should not allow link to be set to "http://"
Failure/Error: should_not allow_value(bad_link).for(:link)
Expected errors when link is set to "http://",
got no errors
# ./spec/models/job_spec.rb:14:in `block (4 levels) in <top (required)>'
I find the only way to for that spec to pass with Ruby 2.2.0 is to include the validates_url gem in my project!!
Does anyone know this is about?
Maybe my solution isn't ideal, but it works.
Replace validates_url gem by validates gem. It has UrlValidator (written by me), which is well tested.
gem 'validates' # in Gemfile
validates :link, :url => true # you needn't to change something. Just remove validates_url from your Gemfile
P.S. It's a strange way - to test functionality of gem. Functionality should be tested in gem already.
P.P.S. I'm strongly recommend you to move to ruby 2.2.1 (or 2.2.2) instead of 2.2.0, because of 2.2.0 has a lot of bugs

The rspec test fails while i am using mongodb database

I am using rspec-rails-3 and my version of rails is 4.0.2. I have mongodb database. While i am trying to run rspec tests, i am getting error
Failure/Error: ext_wiki = Entity.find_by(name_ref:'dev_extraction for wikipedia')
Optionable::Unknown:
:consistency is an unknown option. Valid options are: :write, :read, :database, :max_retries, :pool_size, :retry_interval, :refresh_interval, :down_interval, :ssl, :timeout, :instrumenter, :auto_discover.
I have a Entity named model. Code in entity_spec.rb is as follows -:
require 'rails_helper'
RSpec.describe Entity, :type => :model do
it "checks old and new code" do
ext_wiki = Entity.find_by(name_ref:'dev_extraction for wikipedia')
ext_wiki1 = Entity.find_by(name_ref:'dev_extraction for wikipedia')
expect(ext_wiki1['code']).to eq(ext_wiki['code'])
end
it "gives pass" do
expect(1).to eq(1)
end
end
Your Mongoid configuration (mongoid.yml) has an option (consistency) which is not a valid option. The 4.0.0 changelog says:
The :consistency option is no longer valid, use the :read option now.
Even I change consistency to :read, it didn't work. What I did is I remove the line "consistency: :strong" line from mongoid.yml

why does this not work in Rspec zombie.should_not be_valid

app/models/zombie.rb
class Zombie < ActiveRecord::Base
attr_accessible :name
validates :name, presence: true
end
spec/models/zombie_spec.rb
require 'spec_helper'
describe Zombie do
it "is invalid without a name" do
zombie = Zombie.new
zombie.should_not be_valid
end
end
errors
Zombie
is invalid without a name (FAILED - 1)
Failures:
1) Zombie is invalid without a name
Failure/Error: zombie.should_not be_valid
ActiveRecord::StatementInvalid:
Could not find table 'zombies'
# ./spec/models/zombie_spec.rb:5:in `new'
# ./spec/models/zombie_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 0.02912 seconds
7 examples, 1 failure
Failed examples:
rspec ./spec/models/zombie_spec.rb:4 # Zombie is invalid without a name
Randomized with seed 12906
You should not be defining an initialize method in ActiveRecord classes.
When I have the initialize method defined, this error comes up:
Failure/Error: zombie = Zombie.new
NoMethodError:
undefined method `delete' for nil:NilClass
Without it, it passes as you expect it.
So, change your model to the following and your specs will pass.
class Zombie < ActiveRecord::Base
attr_accessible :name
validates :name, presence: true
end
OR... if you feel you HAVE to, make sure you call super first
def initialize(options={})
super
self.name = options[:name]
end
have you run rake db:test:load so the schema of the db is loaded on the testing db?
also your test is not right, you are testing that your model is invalid but you are not testing that your model is invalid BECAUSE it's name is not present, you should do something like
it "is invalid without a name" do
zombie = Zombie.new
zombie.should_not be_valid
zombie.errors[:name].should_not be_blank
end
that way you really know that the name attribute has an error
Thanks everyone, I was not running migrations so running rake db:migrate fixed it
I am also working through this tutorial and had the same problem. I raked the migration and still didn't get the desired results. As arieljuod mentioned, running $ rake db:test:load resolves the issue.

NameError: uninitialized constant Factory

I'm following this tutorial to get started with TDD on rails with factory girl, rspec and i ran into this issue i can't get my head around.
Here's my "factory".rb (events.rb)
require 'faker'
FactoryGirl.define do
factory :event do
name "HIGH"
genre "house, techno, idb"
venue_name "Westbourne Studios"
venue_address "4-6 Chamberlayne Road"
venue_postcode "NW103JD"
begin_time "10pm"
end_time "2am"
user_id 2
description "A Super massive party with loads of everything you would want around."
status true
venue_id nil
end
end
and here's the event_spec.rb:
require 'spec_helper'
require 'factory_girl_rails'
describe Event do
it "has a valid factory" do
Factory.create(:event).should be_valid
end
it "is invalid without a name"
it "is invalid without a genre"
it "is invalid without a venue_name"
it "is invalid without a venue_address"
it "is invalid without a venue_postcode"
...
end
I have setup the model, migrated etc.. and when i run "rspec spec/models/event_spec.rb" i get the following error:
Failures:
1) Event has a valid factory
Failure/Error: Factory.create(:event).should be_valid
NameError:
uninitialized constant Factory
# ./spec/models/event_spec.rb:7:in `block (2 levels) in <top (required)>'
Finished in 0.1682 seconds
13 examples, 1 failure, 12 pending
Failed examples:
rspec ./spec/models/event_spec.rb:6 # Event has a valid factory
Randomized with seed 64582
Try to use it in this way:
FactoryGirl.create(:event).should be_valid
I think, I can remember, that it was only "Factory" in old versions of the gem. If you take a look in the recent "Getting started" guide of Factory Girl, there are only calls with "FactoryGirl".
If you create file spec/support/factory_girl.rb with content:
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
Then you can simply use:
create(:event)
build(:book)
Instead of:
FactogyGirl.create(:event)
FactogyGirl.build(:book)
I had the same error with factory bot, and to supplement Stefan's answer I came across this little cheat sheet.
https://devhints.io/factory_bot

Resources