I cannot run rpsec after merging my code from Github.
I keep getting FactoryGirl error :
uninitialized constant ControllerMacros::FactoryGirl
1) Question has a valid factory
Failure/Error: expect(FactoryGirl.build(:question)).to be_valid
NameError:
uninitialized constant FactoryGirl
# ./spec/models/question_spec.rb:14:in `block (2 levels) in '
Please help.
It appears that FactoryGirl may be missing some definitions. Try declaring the following in your spec_helper:
# spec/spec_helper.rb
require 'factory_girl'
FactoryGirl.find_definitions
If this is for a Rails project, you can avoid finding definitions manually by using factory_girl_rails, which will automatically load your definitions for you.
Related
My attendance_rspec.rb file contains:
require "rails_helper"
RSpec.describe Attendance, type: :model do
it { should have_db_column(:date).of_type(:date) }
end
When I run it from the terminal using rspec spec/models/attendance_spec.rb
It shows the error:
Failures:
1) Attendance
Failure/Error: it { should have_db_column(:date).of_type(:date) }
NoMethodError:
undefined method `of_type' for #<RSpec::Matchers::BuiltIn::Has:0x000055e5c65dd3e0>
# ./spec/models/attendance_spec.rb:4:in `block (2 levels) in <main>'
I already have FactoryBot configured along with database_cleaner and capybara gems. What gem am I missing or any config missing in rails_helper.rb?
I followed this link for setting up Rspec
As NickM pointed out, the have_db_column matcher is provided by the shoulda-matchers gem. Add it to your Gemfile and follow the integration steps to use have_db_column.
I generated the following scaffold (using Ruby 2.2.0, rails 4.1.8, postgres):
rails g scaffold Test user:references text:references data:hstore
In my test_spec.rb:
require 'rails_helper'
RSpec.describe Test, :type => :model do
describe 'User generates a test' do
before do
#text = create(:text, content: "Cats eat mice")
#user = create(:user)
#test = create(:test)
end
...
When I run rspec the test fails with the following message:
Failure/Error: #test = create(:test)
NoMethodError:
undefined method `new' for Test:Module
# ./spec/models/test_spec.rb:8:in `block (3 levels) in <top (required)>'
When I test other models (user, text) everything works well, only the Test model fails. Calling Test.create(...) in rspec file also fails. Creating new test in rails console works. Any ideas how to fix this?
With the default configuration, Rails defines the module constant Test, so Ruby doesn't autoload your test.rb file when FactoryGirl does a Test.new as part of your :test factory.
You can install Rails without the Test infrastructure by using the -T switch, in which case it won't define the Test module and you should be fine.
If Rails is already configured, you can put the following in your rails_helper.rb file to remove the Test constant and you should be ok as well:
Object.send(:remove_const, :Test)
I'm fairly new to rails and testing. Documentation says to add this code to spec_helper.rb:
RSpec::Sidekiq.configure do |config|
config.warn_when_jobs_not_processed_by_sidekiq = false
end
but when I do, i get an error:
uninitialized constant RSpec::Sidekiq (NameError)
If you have rspec 3, then you should try to add following code to rails_helper.rb instead of spec_helper.rb
I'm trying to build some tests with Rspec and I'm getting the following error:
Failure/Error: get :index
NoMethodError:
undefined method `authenticate!' for nil:NilClass
Then I made some searchs and I realized that I had to include this line in the spec_helper.rb:
RSpec.configure do |config|
config.include Devise::TestHelpers, type: :controller
end
It seems that this code worked for most people but not for me. Now i'm getting the following error:
/home/bruna/Dropbox/Rails/academico/spec/spec_helper.rb:18:in `block in <top (required)>': uninitialized constant Devise (NameError)
I think this might be some error in this version of rspec and/or devise. Has anyone seen this error? Please, help me :'(
RSpec 3 configuration is different from earlier versions, with the addition of a spec/rails_helper.rb file that must be included for each spec file. Add the Devise TestHelpers to the spec/rails_helper.rb file.
If you need more information about RSpec 3 configuration, I've written an RSpec Tutorial. There's also information about testing Devise with RSpec, including advice about session helpers for feature testing, in my Rails Devise Tutorial.
I'm following this example here: http://erikonrails.snowedin.net/?p=230
I added:
lib/delayed_job_spec_helper.rb
Then in my rspec I have:
describe Thingy do
include DelayedJobSpecHelper
it "should have been worked on if I do something that queues jobs" do
thing = Thingy.new
thing.method_that_queues_jobs
work_off
thing.should be_worked_on
end
end
Problem is I get the error:
/Library/Ruby/Gems/1.8/gems/aws-s3-0.6.2/lib/aws/s3/extensions.rb:206:in `const_missing': uninitialized constant DelayedJobSpecHelper (NameError)
Ideas? thanks
Please check this discussion, it might be connected to your problem.
http://www.mail-archive.com/thinking-sphinx#googlegroups.com/msg05036.html