NoMethodError: undefined method `validate_presence_of' (Rspec and Shoulda-Matchers) - ruby-on-rails

I'm trying to set up Rspec and Shoulda-Matchers, but for some reason I get this error:
NoMethodError:
undefined method `validate_presence_of' for #RSpec::ExampleGroups::AdCampaign::Validations:0x000000062a2b90>
Failure/Error: it { should have_many :advertisements }
expected # to respond to has_many?
NoMethodError:
undefined method `belong_to' for #RSpec::ExampleGroups::AdCampaign::Associations:0x0000000686d8e0>
It seems that I tried every answer from stackoverflow and github issues, and nothing helps.
Maybe you could help me to find out what I'm doing wrong?
Here's my rails_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'shoulda/matchers'
require 'spec_helper'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
and here's my spec_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'shoulda/matchers'
require 'database_cleaner'
require 'capybara/rspec'
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.include Rails.application.routes.url_helpers
config.include FactoryGirl::Syntax::Methods
config.include(Shoulda::Matchers::ActiveModel, type: :model)
config.include(Shoulda::Matchers::ActiveRecord, type: :model)
# config.include Shoulda::Matchers::ActiveRecord, type: :model
# config.include Devise::TestHelpers, type: :controller
config.order = 'random'
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
heer's the spec file with my tests:
require 'rails_helper'
RSpec.describe AdCampaign, type: :model do
describe 'validations' do
it { should validate_presence_of :shop }
it { should validate_presence_of :description }
end
describe 'associations' do
it { should belong_to :shop }
it { should have_many :advertisements }
it { should have_one :recipient_list }
end
end
and here's the model I'm trying to test:
class AdCampaign < ActiveRecord::Base
belongs_to :shop
has_many :advertisements
has_one :recipient_list
validates :shop, :description, presence: true
end
I've tried placing Shoulda::Matchers.configure do... in both rails_helper.rb and spec_helper.rb.
And in my gem file I have shoulda-matchers gem like this:
group :development, :test do
gem 'shoulda-matchers', require: false
end
Could you please help me with setting up shoulda-matchers and rspec? What am I doing wrong here?

I had a similar problem! Me helped this:
# Gemfile
group :test do
gem "shoulda-matchers", require: false
end
And this is to add to the top rspec_helper.rb
require 'shoulda-matchers'
require "bundler/setup"
::Bundler.require(:default, :test)
::Shoulda::Matchers.configure do |config|
config.integrate do |with|
# Choose a test framework:
with.test_framework :rspec
#with.test_framework :minitest
#with.test_framework :minitest_4
#with.test_framework :test_unit
# Choose one or more libraries:
with.library :active_record
with.library :active_model
#with.library :action_controller
# Or, choose the following (which implies all of the above):
#with.library :rails
end
end

Related

Why rspec doesn't run single spec?

When I trying to run single test, like
bundle exec rspec spec/some_path/some_spec.rb
It runs all specs anyway, like it would be if I run bundle exec rspec spec/
Even when it print out failed specs and I only copy failed examples and put it again, the Rspec will run all existed specs. I haven't found any information about such behaviour in rspec documentation. Thank you very much if you know, what is wrong with my code! (or configuration, I suppose)
spec_helper.rb:
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.shared_context_metadata_behavior = :apply_to_host_groups
config.filter_run_when_matching :focus
config.disable_monkey_patching!
if config.files_to_run.one?
config.default_formatter = 'doc'
end
config.profile_examples = 10
config.order = :random
Kernel.srand config.seed
end
require 'webmock/rspec'
def body_as_json
json_str_to_hash response.body
end
def json_str_to_hash(str)
JSON.parse(str).with_indifferent_access
end
.rspec:
--require rails_helper
--format progress
--color
--order random
--profile 10
rails_helper.rb
# frozen_string_literal: true
unless ENV['COVERAGE'].to_s == ''
require 'simplecov'
SimpleCov.start 'rails'
end
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../config/environment', __dir__)
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/rails'
require 'capybara/poltergeist'
require 'capybara-screenshot/rspec'
require 'timecop'
require 'sidekiq/testing'
require 'database_cleaner'
require 'webmock/rspec'
require 'audited-rspec'
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
ActiveRecord::Base.connection.reconnect!
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.infer_base_class_for_anonymous_controllers = false
config.include Capybara::DSL
config.include FactoryBot::Syntax::Methods
config.include CapybaraHelpers
config.include ProcessingHelpers
config.include RequestsHelper, type: :request
config.include ApiHelper
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with :truncation, except: %w[spatial_ref_sys schema_migrations]
Faker::Config.locale = :en
# Rails.application.load_seed
Sidekiq::Testing.fake!
end
config.around do |example|
DatabaseCleaner.cleaning do
example.run
end
end
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(
app,
phantomjs_logger: Rails.root.join('log', 'poltergeist.log'),
inspector: true
)
end
Capybara.javascript_driver = :poltergeist
Capybara.server_port = 3001
Capybara.app_host = 'http://localhost:3001'

Rspec - uninitialized constant error User

I have written tests on Rspec for my model User and get error while starting 'rspec spec'
NameError: uninitialized constant User
my test spec/models/ivd/user_spec.rb
require 'rails_helper'
module Ivd
RSpec.describe User, type: :model do
let(:user) { FactoryGirl.create(:ivd_user, email: "user#example.org", password: "very-secret", admin: true) }
it 'has a valid factory' do
expect(user).to be_valid
end
describe '.find_for_oauth' do
let!(:user) { FactoryGirl.create(:ivd_user) }
describe 'twitter' do
let(:auth) { OmniAuth::AuthHash.new({provider: 'twitter', uid: '12345'})}
context 'user has already authorization' do
it 'return user' do
user.identities.create({provider: 'twitter', uid: '12345'})
expect(User.find_for_oauth(auth)).to eq user
end
end
end
end
end
end
spec_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require 'simplecov'
# SimpleCov.formatter = SimpleCov::Formatter::HTMLFormatter
SimpleCov.start :rails do
add_filter do |source_file|
source_file.lines.count < 5
end
end
require File.expand_path('../dummy/config/environment.rb', __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'factory_girl_rails'
require 'capybara/poltergeist'
# require 'capybara/rails'
require 'ivd/seeder'
# http://www.thegreatcodeadventure.com/stubbing-with-vcr/
require 'vcr'
require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)
# load(Rails.root.join("db", "seeds.rb"))
# Configure capybara for integration testing
# Capybara.default_driver = :rack_test
# Capybara.default_selector = :css
# js_options = {js_errors: false}
# above is sometimes useful to troubleshoot errors with tests
js_options = {}
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, js_options)
end
Capybara.javascript_driver = :poltergeist
# Capybara.ignore_hidden_elements = false
# http://stackoverflow.com/questions/24078768/argumenterror-factory-not-registered
# as per above, need to explicitly set below
FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)]
FactoryGirl.find_definitions
# Oddly above does not occur if factory_girl_rails is only referrenced in ivd.gemspec
# but not main gemfile
Rails.backtrace_cleaner.remove_silencers!
# Load support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
ActionController::Base.prepend_view_path "#{Ivd::Engine.root}/app/themes/default/views/"
RSpec.configure do |config|
# TODO - consider precompiling assets to speed up tests
# config.before(:suite) do
# Rails.application.load_tasks
# Rake::Task["assets:precompile"].invoke
# end
config.include JsonSpec::Helpers
config.warnings = false
config.mock_with :rspec
config.infer_base_class_for_anonymous_controllers = false
config.order = 'random'
# config.include Ivd::ApplicationHelper
# config.include Rails.application.routes.url_helpers
# config.include Ivd::Engine.routes.url_helpers
config.use_transactional_fixtures = false
#
# Make sure the database is clean and ready for test
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
Ivd::Seeder.seed!
end
config.after(:all) do
# http://renderedtext.com/blog/2012/10/10/cleaning-up-after-before-all-blocks/
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, js: true) do
# truncation is slower but more reliable
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
rails_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../dummy/config/environment.rb', __FILE__)
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'rails-controller-testing'
# Add additional requires below this line. Rails is not loaded until this point!
require 'devise'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.warnings = false
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{Ivd::Engine.root}/spec/fixtures"
# above used by fixture_file_upload
# eg in:
# /Users/etewiah/Ed/sites-2016-oct-ivd/ivd/spec/services/ivd/import_properties_spec.rb
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
[:controller, :view, :request].each do |type|
config.include ::Rails::Controller::Testing::TestProcess, type: type
config.include ::Rails::Controller::Testing::TemplateAssertions, type: type
config.include ::Rails::Controller::Testing::Integration, type: type
end
# https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec)
config.include Devise::Test::ControllerHelpers, type: :controller
config.include FeatureHelpers, type: :feature
config.extend ControllerMacros, type: :controller
# https://github.com/plataformatec/devise/wiki/How-To:-Stub-authentication-in-controller-specs
config.include ControllerHelpers, type: :controller
# https://github.com/plataformatec/devise/wiki/How-To:-sign-in-and-out-a-user-in-Request-type-specs-(specs-tagged-with-type:-:request)
config.include RequestSpecHelpers, type: :request
end
I have model User, which has been inherited from ApplicationRecord.
File .rspec contains only:
--color
--require byebug
--require rails_helper
Is your model itself within the module Ivd? i.e.
module Ivd
class User
If so, you want to be using RSpec.describe Ivd::User rather than wrapping the call in the module.

Rails 5.1: Rspec can't see Capybara features

I have some feature specs in my project that look roughly like this:
# specs/features/canvas_integration.rb
require 'spec_helper'
feature 'Routes to default page' do
let!(:mode) { create(:mode) }
scenario 'as anyone' do
visit root_path
expect(page).to have_content('Your home')
end
end
But running the specs results in nothing:
rspec spec/features
No examples found.
My spec_helper.rb looks like this:
# frozen_string_literal: true
require 'simplecov'
SimpleCov.start 'rails'
require File.expand_path('../../config/environment', __FILE__)
require 'codeclimate-test-reporter'
require 'rspec/rails'
require 'webmock/rspec'
require 'devise'
require 'codeship'
require 'capybara/rspec'
require 'capybara/rails'
WebMock.disable_net_connect!(allow_localhost: true)
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::ControllerHelpers, type: :view
config.include FactoryGirl::Syntax::Methods
config.extend ControllerMacros, type: :controller
config.include Macros
config.color = true
config.fixture_path = "#{::Rails.root}/spec/factories/fixtures"
config.infer_spec_type_from_file_location!
config.infer_base_class_for_anonymous_controllers = false
config.order = 'random'
config.warnings = false
config.default_formatter = 'doc' if config.files_to_run.one?
config.profile_examples = 10
config.use_transactional_examples = false
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
# config.before(:each, type: :system) { driven_by :rack_test }
config.mock_with :rspec do |mocks|
mocks.verify_doubled_constant_names = true
end
config.expect_with :rspec do |c|
c.syntax = :expect
end
end
Why can't Rspec find my features?
Your files need to end in _spec.rb in order for RSpec to load them when specifying a directory. So rename to specs/features/canvas_integration_spec.rb
Also note that in Rails 5.1 database cleaner is probably not necessary (if you're using one of the usual databases) - so you should be able to remove/comment out that and re-enable transactional testing. Additionally use_transactional_examples is an alias of use_transactional_fixtures so specifying both doesn't make sense.
I think your test spec filename should be specs/features/canvas_integration_spec.rb instead of specs/features/canvas_integration.rb
Why?
The files read by the rspec-rails gem are those that end in _spec.rb

Why do I receive this RSpec error when I try to create a factory?

**Update*: Scroll to very bottom for reproducible error in bare minimum rails app*
I am testing a rails task with Rspec, the fantaskspec gem, and the Factory Girl. When I try to run tests I receive this error:
1) update_account_reverification_table update_account_reverification_table
Failure/Error: account = create(:account, twitter_id: 1234567890)
NoMethodError:
undefined method `create' for #<RSpec::ExampleGroups::UpdateAccountReverificationTable:0x007fda1bd07710>
# ./spec/lib/tasks/account_reverification_spec.rb:18:in `block (2 levels) in <top (required)>'
On line, 18 of my account_reverification_spec.rb file I have this test.
it 'update_account_reverification_table' do
# binding.pry
account = create(:account, twitter_id: 1234567890)
Account.first.reverification.twitter_reverification_sent_at.must_equal Time.now
Account.first.reverification.twitter_reverified.must_equal true
end
I have also tried this variation but the test continues to fail:
account = FactoryGirl.create(:account, twitter_id: 1234567890)
However I receive a different error:
1) update_account_reverification_table update_account_reverification_table
Failure/Error: account = FactoryGirl.create(:account, twitter_id: 1234567890)
ArgumentError:
Factory not registered: account
Here is my spec_helper.rb and my rails_helper.rb
require 'fantaskspec'
require 'factory_girl_rails'
require 'pry-rails'
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.infer_rake_task_specs_from_file_location!
end
and rails_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.include FactoryGirl::Syntax::Methods
FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)]
FactoryGirl.find_definitions
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
end
Note that I have these lines in my rails_helper.rb file:
config.include FactoryGirl::Syntax::Methods
FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)]
FactoryGirl.find_definitions
I grabbed this from a previous answer on a different problem referenced here Specific config problem Any ideas?
-----EDIT #2------
I have created a brand new basic example app to help figure out where this problem could potentially be. I have followed the instructions to set up my directory and tree structure using these documentation links.
Rspec
Factory Girl Rails
Fantaskspec
Tutorial on Rspec and Fantaskspec
Directory tree structure:
spec
- factories
- accounts.rb
- lib
- tasks
- account_reverification_spec.rb
- support
- factory_girl.rb
- rails_helper.rb
- spec-helper.rb
Contents of spec/support/factory_girl.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
Contents of spec/factories/accounts.rb
FactoryGirl.define do
sequence :account_login do |n|
"login-#{ n }"
end
factory :account do
sequence :email do |n|
"someone#{n}#gmail.com"
end
email_confirmation { |account| account.send :email }
url { Faker::Internet.url }
login { generate(:account_login) }
password { Faker::Internet.password }
password_confirmation { |account| account.send(:password) }
current_password { |account| account.send(:password) }
twitter_account 'openhub'
name { Faker::Name.name + rand(999_999).to_s }
about_raw { Faker::Lorem.characters(10) }
activated_at { Time.current }
activation_code nil
country_code 'us'
email_master true
email_kudos true
email_posts true
end
end
Contents of spec/lib/tasks/account_reverification_spec.rb
require 'rails_helper'
RSpec.describe 'update_account_reverification_table' do
let(:task_name) { 'update_account_reverification_table' }
it 'correct task is called' do
expect(subject).to be_a(Rake::Task)
expect(subject.name).to eq("update_account_reverification_table")
expect(subject).to eq(task)
end
it 'update_account_reverification_table' do
account = create(:account, twitter_id: 1234567890)
end
end
Contents of rails_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'fantaskspec'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
Rails.application.load_tasks
config.infer_rake_task_specs_from_file_location!
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
end
Contents of spec_helper.rb
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
Try adding the following line to your spec_helper.rb file:
require 'factory_girl'
Also, add these lines to your spec_helper.rb:
FactoryGirl.definition_file_paths.clear
FactoryGirl.definition_file_paths << File.expand_path('../factories', __FILE__)
FactoryGirl.find_definitions
and remove them from RSpec.configure do |config| block.
Also, make sure you defined your factories in the correct file. It should be defined in factories.rb in your test folder (spec) if you use rspec.
Update
FactoryGirl.definition_file_paths.clear
FactoryGirl.definition_file_paths << "./spec/factories"
FactoryGirl.find_definitions
These lines should go to your spec/support/factory_girl.rb file and remove them from anywhere else.
Also, add the following in your spec_helper.rb:
config.before(:all) do
FactoryGirl.reload
end
In your spec_helper.rb the Rspec.configure block should look like this:
RSpec.configure do |config|
config.mock_with :rspec
config.run_all_when_everything_filtered = true
config.filter_run :focus
config.include FactoryGirl::Syntax::Methods
end
Update
Add this to your rails_helper.rb:
require_relative 'support/factory_girl.rb'
And then it should work.

'Factory not registered' error in Rails 4

I am getting a 'Factory not registered' error using Factory Girl in Rails 4. Here is my factory definition for a simple Devise user in spec/factories/user.rb:
FactoryGirl.define do
factory :user do
email 'test#example.com'
password 'foobar'
password_confirmation 'foobar'
end
end
Here is the spec_helper.rb:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'email_spec'
require 'rspec/autorun'
require 'factory_girl'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.include(EmailSpec::Helpers)
config.include(EmailSpec::Matchers)
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.include Devise::TestHelpers, :type => :controller
config.include FactoryGirl::Syntax::Methods
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
And in the gem file:
gem 'factory_girl_rails', :require => false
And here is the test in spec/models/user_spec.rb:
require 'spec_helper'
describe User do
it "has a valid factory" do
FactoryGirl.build(:user).should be_valid
end
end
This fails, saying 'Factory not registered: user'. I've tried moving the Factory definition to spec/factories.rb, as well as using just factory_girl instead of factory_girl_rails, with no luck. Any suggestions would be greatly appreciated.
You're requiring factory_girl instead of factory_girl_rails. Edit the equivalent line in your spec_helper.rb to this:
require 'factory_girl_rails'

Resources