In gem file I write this:
group :test do
gem 'rspec-rails'
gem 'shoulda-matchers', require: false
gem 'factory_girl'
end
My rails_helper
require 'rspec/rails'
require 'shoulda/matchers'
My spec_helper
require 'rails_helper'
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.include FactoryGirl::Syntax::Methods
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
# Choose a test framework:
with.test_framework :rspec
# Choose one or more libraries:
with.library :active_record
with.library :active_model
with.library :action_controller
end
end
And finally my test:
require 'spec_helper'
describe Person do
it {should validate_presence_of(:identifier)}
end
And I get nomethoderror
Failure/Error: it {should validate_presence_of(:identifier)}
NoMethodError:
undefined method `validate_presence_of' for #<RSpec::ExampleGroups::Person:0x007fe58d13ca20>
I simply don't get it. In other project the same way works. Here it doesn't. I have spent two days to fix this problem. Are there better alternatives of Shoulda Matchers in rails?
Sorry you're having trouble. You need to load Rails before you require shoulda-matchers. The gem will only make model matchers available if it can see that ActiveRecord is available; if not, then you won't get them.
It appears that you have rails_helper and spec_helper swapped. Typically in a fresh project with rspec-rails installed, rails_helper will load Rails and then require spec_helper. I would remove both files (or move them aside so you still have them) and then re-run rails g rspec:install to regenerate them. You'll still want to add the shoulda-matchers configuration block to rails_helper as you have done, but by that point ActiveRecord (and the rest of Rails) should be loaded.
Change your Gemfile to use two different groups for test like this:
group :test, :development do
gem 'rspec-rails'
gem 'factory_girl'
end
group :test do
gem 'shoulda-matchers', require: false
end
This used to be caused when shoulda would load with Test::Unit instead of RSpec, but I thought that was fixed.
Related
I tried to use shoulda-matchers to test the association between models. However, it always show Error:
TakingTest#test_belongs_to:
NoMethodError: undefined method belong_to' for #<TakingTest:0x00007fc14b8e64a8>
test/models/taking_test.rb:8:inblock in '
I checked other answer, most of them are at least 4 years ago. Does it still work with rails 6.0?
ruby '2.6.5'
rails', '~> 6.0.2'
gem file
group :development, :test do
gem 'rspec-rails'
end
group :test do
gem 'shoulda', '~> 3.5'
gem 'shoulda-matchers'
end
spec/rails_helper.rb:
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
test/models/taking_test.rb
class TakingTest < ActiveSupport::TestCase
test "belongs to" do
should belong_to(:students)
end
end
Having both a spec directory and a test directory could be causing your problem. IMO, there should only ever be spec or test for a every project, never both.
Usually, test files begin with an include for the test_helper.rb file.
require 'test_helper'
Instead of a test_helper, you've got a spec_helper.
Try moving the Shoulda initializer from spec/spec_helper.rb to test/test_helper.rb
I have recently upgraded my ruby on rails project to rails 5.0.7 and ruby 2.5.1 and I am getting an RSPEC error undefined methodexpects' for` different objects I am testing.
I have tried adding a configuration in the spec_helper.rb file as suggested here (although I did a quick search and didn't find :should defined anywhere), here and here:
config.expect_with :rspec do |expectations|
expectations.syntax = [:expect, :expects]
end
Also tried including include RSpec::Matchers in my spec_helper.rb, but then I get even more errors:
`only the `receive`, `have_received` and `receive_messages` matchers are supported with `expect(...).to`, but you have provided: #<RSpec::Matchers::BuiltIn::Eq:0x00007f962b3db058>`
An example of how I am using expect:
describe "process" do
before :each do
#item = Item.new
end
it "parses the uploaded file and extract the correct report data" do
item_a_data = {"name" => "one"}
item_b_data = {"name" => "two"}
file_contents = {"items" => [item_a_data, item_b_data]}
#item.data_file = double("file", :read => file_contents.to_json)
#item.name = item_a_data["name"]
#item.expects(:interpret_json_file).with(#item.data_file).returns(file_contents)
#item.expects(:save).returns(true)
expect(#item.process_data_file).to be_truthy
expect(#item.data).to eq item_a_data.to_json
end
Notice that the error occurs when I call #item.expects
In my Gemfile I have the following gems (among others):
group :development, :test do
gem 'byebug'
gem 'sqlite3'
gem 'rspec-rails'
gem 'rb-fsevent', '~> 0.9.1'
gem 'guard-rspec', '4.7.3'
gem 'guard-spork', '2.1.0'
gem 'spork', '0.9.2'
gem 'jasmine-rails'
gem 'teaspoon-jasmine'
end
group :test do
gem 'capybara'
gem 'cucumber-rails', :require => false
gem 'database_cleaner'
gem 'factory_girl_rails', '4.1.0'
gem 'launchy'
gem 'poltergeist'
gem 'timecop'
gem 'webmock'
gem 'simplecov', '~> 0.16.0'
gem 'rails-controller-testing'
end
spec_helper.rb:
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
require 'simplecov'
SimpleCov.start 'rails'
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'devise'
require './spec/controllers/controller_helpers.rb'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
config.include ControllerHelpers, type: :controller
config.include Capybara::DSL
# 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 = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
config.infer_spec_type_from_file_location!
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.include FactoryGirl::Syntax::Methods
# config.expect_with :rspec do |expectations|
# expectations.syntax = :expect
# end
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
The solution would be to re-write it to the following syntax:
expect(#item).to receive(:interpret_json_file).with(#item.data_file) { file_contents }
expect(#item).to receive(:save) { true }
Basically ActiveRecord model does not know about rspec expectations and it should not. If that specs were working before then you might used some decoration for ActiveRecord models and that is why it worked previously.
I am using rspec-rails, factory_girl_rails and mogoid_rspec gems. After adding factory girl gem, I keep getting the error Factory not registered: user (ArgumentError) for my user factory. Following are the related code snippets:
In my Gemfile:
group :development, :test do
gem 'byebug'
gem 'rspec-rails'
gem 'mongoid-rspec', '~> 2.1.0'
end
group :test do
gem 'database_cleaner'
gem 'faker'
gem 'shoulda-matchers'
gem 'factory_girl_rails'
end
rails_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
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 'factory_girl_rails'
FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)]
FactoryGirl.find_definitions
require 'support/mongoid'
require 'support/factory_girl'
require 'support/disable_active_record_fixtures'
require 'mongoid-rspec'
ENV["RAILS_ENV"] ||= 'test'
RSpec.configure do |config|
config.before(:all) do
FactoryGirl.reload
end
config.include Mongoid::Matchers, type: :model
config.before(:suite) do
DatabaseCleaner[:mongoid].strategy = :truncation
end
config.before(:each) do
DatabaseCleaner[:mongoid].start
end
config.after(:each) do
DatabaseCleaner[:mongoid].clean
end
config.use_transactional_fixtures = false
config.infer_spec_type_from_file_location!
end
factories/user.rb
FactoryGirl.define do
factory :user do
first_name Faker::Name.first_name
last_name Faker::Name.last_name
email Faker::Internet.email
end
end
spec/support/factory_girl.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
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
Any help would be much appreciated. Thanks in advance! :)
As we've discussed, I'm pasting my proposition of what you could change in your code (including good practices).
Define factory_girl_rails gem inside group :test, :development.
#Gemfile
group :development, :test do
gem 'byebug'
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'faker'
gem 'mongoid-rspec', '~> 2.1.0'
end
group :test do
gem 'database_cleaner'
gem 'shoulda-matchers'
end
Require your support files in one single line instead of multiply
definitions.
# spec/rails_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
RSpec.configure do |config|
config.before(:all) do
FactoryGirl.reload
end
config.include Mongoid::Matchers, type: :model
config.before(:suite) do
DatabaseCleaner[:mongoid].strategy = :truncation
end
config.before(:each) do
DatabaseCleaner[:mongoid].start
end
config.after(:each) do
DatabaseCleaner[:mongoid].clean
end
config.use_transactional_fixtures = false
config.infer_spec_type_from_file_location!
end
Create your .rspec file (or edit yours if you already have it) to
require spec_helper and rails_helper without having to call it manually in every single spec.
I'd also suggest for everyone to
color and format the spec output.
# .rspec
--color
--format documentation
--require spec_helper
--require rails_helper
I have the followed
group :test, :development do
gem 'rspec-rails', '~> 3.0'
gem "guard-rspec"
gem 'capybara'
gem 'factory_girl_rails'
gem 'turn'
gem 'guard' # NOTE: this is necessary in newer versions
gem 'poltergeist'
gem 'phantomjs', :require => 'phantomjs/poltergeist'
gem 'selenium-webdriver'
gem 'capybara-screenshot'
gem 'capybara-webkit'
gem 'zeus'
gem "parallel_tests"
gem 'launchy'
gem 'email_spec'
gem 'action_mailer_cache_delivery'
gem 'protractor-rails'
gem 'database_cleaner'
and in my spec_helper.rb
require 'capybara/rspec'
require 'factory_girl_rails'
require 'support/request_helpers'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.include RequestHelpers
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
Finally I have such a spec helper in the spec/support/request_helper.rb
module RequestHelpers
def create_logged_in_user
user = create(:user)
login(user)
user
end
def login(user)
login_as user, scope: :user
end
end
and factories.rb
# /spec/factories/factories.rb
FactoryGirl.define do
factory :user do
first "John"
last 'Smith'
email 'test#example.com'
password 'johnsmith123'
end
end
Yes everytime I run rpsec it shows that
Factory not registered: user
And when I rune FactoryGirl.factories in the rails console, i can see 'user' is registered
=> #<FactoryGirl::Registry:0x007fa4e0e18160
#items=
{
:user=>
#<FactoryGirl::Factory:0x007fa4e2371200
#aliases=[],
#class_name=nil,
#compiled=false,
#definition=
#<FactoryGirl::Definition:0x007fa4e2370f58
#additional_traits=[],
#attributes=nil,
#base_traits=[],
#callbacks=[],
#compiled=false,
#constructor=nil,
#declarations=
#<FactoryGirl::DeclarationList:0x007fa4e2370ee0
#declarations=
[#<FactoryGirl::Declaration::Static:0x007fa4e2370b48 #ignored=false, #name=:first, #value="john">,
#<FactoryGirl::Declaration::Static:0x007fa4e2370a80 #ignored=false, #name=:last, #value="smith">,
#<FactoryGirl::Declaration::Static:0x007fa4e23709b8 #ignored=false, #name=:email, #value="test#example.com">,
I read through the Github setup docs for factory_girl_rails, rspec-rails multiple times but found no solution.
Does anyone know where I should look to detect the problem?
Thanks!
EDITED added contents of spec/rails_helper.rb updated Wed, 10 Jun 2015
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'capybara/poltergeist'
require 'capybara-screenshot/rspec'
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, :phantomjs => Phantomjs.path, :inspector => true)
end
Capybara.javascript_driver = :webkit
include Warden::Test::Helpers
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
end
I had the same issue
Why is FactoryBot not finding our factories only inside RSpec, I do not know.
But making FactoryBot explicitly find my definitions worked.
So you just need to add this to your spec_helper:
FactoryBot.find_definitions
in my case, I followed the general instructions in this answer:
https://stackoverflow.com/a/49436531/8790125
So I create a file spec/support/factory_bot.rb
with this code (note the find_definitions):
require 'factory_bot'
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
FactoryBot.find_definitions
end
and then required on rails_helper:
# ...
require 'rspec/rails'
require 'support/factory_bot'
# ...
I'm adding Rspec tests to a project that didn't have rspec before, or automated tests at all. It is running on a Windows 7 box. When I run rspec, it shows the error:
undefined method `has_attached_file'
The main issue is that I'm writing just a simple test for a model that is not using Paperclip at all. Paperclip is used on another model.
I've researched extensively, trying to include the gem in test environment but nothing seems to solve the issue.
Is there a way of skipping or forcing the inclussion of paperclip when I run the rspec tests?
My gemfile has, among other things, the following:
source 'http://rubygems.org'
ruby "1.9.2"
gem 'rails', '3.0.20'
...
gem "paperclip", :git => 'git://github.com/thoughtbot/paperclip.git'
gem 'aws-s3'
gem 'aws-sdk'
group :test, :development do
gem 'rspec-rails', '~> 2.14.0'
end
group :test do
gem 'sqlite3'
gem 'capybara', '2.0'
gem 'selenium-webdriver'
gem 'shoulda-matchers'
gem 'database_cleaner'
gem 'launchy', '2.2.0'
gem 'factory_girl_rails'
end
I run the rspec command:
bundle exec rake spec
I get the following error:
C:/Users/MAC/.pik/rubies/Ruby-192-p290/bin/ruby.exe -S rspec ./spec/models/filter_category_spec.rb ./spec/models/filter_spec.rb
C:/Users/MAC/.pik/rubies/Ruby-192-p290/lib/ruby/gems/1.9.1/gems/attr_encrypted-1.2.1/lib/attr_encrypted.rb:241:in `method_missing': undefined method `has_a
ttached_file' for #<Class:0x5278770> (NoMethodError)
from C:/Users/MAC/.pik/rubies/Ruby-192-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.0.20/lib/active_record/base.rb:1018:in `method_missing'
from C:/Users/MAC/.pik/rubies/Ruby-192-p290/lib/ruby/gems/1.9.1/gems/attr_encrypted-1.2.1/lib/attr_encrypted/adapters/active_record.rb:50:in `metho
d_missing_with_attr_encrypted'
....
from C:/Users/MAC/.pik/rubies/Ruby-192-p290/lib/ruby/gems/1.9.1/gems/railties-3.0.20/lib/rails/application.rb:77:in `method_missing'
from C:/Avity/DS Candidates Tracking System/ds-cts/config/environment.rb:5:in `<top (required)>'
from C:/Avity/DS Candidates Tracking System/ds-cts/spec/spec_helper.rb:3:in `require'
from C:/Avity/DS Candidates Tracking System/ds-cts/spec/spec_helper.rb:3:in `<top (required)>'
from C:/Avity/DS Candidates Tracking System/ds-cts/spec/models/filter_category_spec.rb:1:in `require'
My spec_helper file is:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
#Database cleaner
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
#Use chrome driver
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Capybara.current_driver = :selenium_chrome
end
Any ideas of what should I do to make this work properly?
EDIT It seems the issue comes before loading spec_helper.rb
If I make changes on this file, like adding errors on purpose, nothing happens and the issue above keeps happening.
The only way I have to run the tests is by commenting the has_attached_file line on my model and adding the group option to the paperclip gem on my Gemfile, to avoid having it on test environment:
gem "paperclip", :git => 'git://github.com/thoughtbot/paperclip.git', :group=>[:development,:production]
Not sure how to solve this without skipping Paperclip from test env.
The problem seems to me to be with shoulda-matchers and paperclip
Add to your spec_helper.rb file
require "paperclip/matchers"
below where you are
require "capybara/rspec"
And add
config.include Paperclip::Shoulda::Matchers
below
RSpec.configure do |config|
Check out the shoulda-matchers documentation for paperclip
Hope this helps
I ran into this issue when trying to use Paperclip in a gem that has ActiveRecord, but not Rails. I was able to fix this for tests by adding a spec/support/paperclip.rb file with the following:
require 'paperclip/railtie'
RSpec.configure do |config|
# Bind paperclip to enable ActiveRecord #has_attached_file method
Paperclip::Railtie.insert
end
As also suggested, this is a good place to add config.include Paperclip::Shoulda::Matchers.
But I'm using Rails 4.2 and Rspec 3, so I think the order of things loaded is much different than yours.