Rspec giving databasecleaner problems in a custom folder - ruby-on-rails

I have opened a folder spec/features. When I launch rspec spec/ everything goes well, but if I do rspec spec/features I get:
An error occurred in a before(:suite) hook.
Failure/Error: DatabaseCleaner[:active_record].strategy = :transaction
NameError:
uninitialized constant DatabaseCleaner
# ./spec/spec_helper.rb:22:in `block (2 levels) in <top (required)>'
what could be happening?

Related

Gettting undefined method `empty?' for nil:NilClass in my test suite when I try to call a Rails path helper (*_path) using Rails, Rspec

Rails 6.1.3.1
Rspec
basic behavior spec code:
describe "index" do
it "should show me the list" do
visit dashboard_targets_path
end
end
the routes file
namespace :dashboard do
resources :targets
end
error shows me the exception, but strangely it appears as if it isn't calling through to the app, just fails right in my test code:
1) interaction for Dashboard::TargetsController index should show me the list
Failure/Error: visit dashboard_targets_path
NoMethodError:
undefined method `empty?' for nil:NilClass
# ./spec/system/dashboard/targets_behavior_spec.rb:16:in `block (3 levels) in <top (required)>'
# /Users/jason/.rvm/gems/ruby-2.6.6/gems/webmock-3.12.2/lib/webmock/rspec.rb:37:in `block (2 levels) in <main>'
# /Users/jason/.rvm/gems/ruby-2.6.6/gems/rspec-wait-0.0.9/lib/rspec/wait.rb:46:in `block (2 levels) in <main>'
it seems to be failing inside the test code, if I drop into the debugger there and run dashboard_targets_path directly I also get the same exception, so the problem is just using the helper within the TEST ENVIRONMENT
within the dev environment, this function works
the problem here was the the config/environments/test.rb file does have default_url_options.
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
when you hit errors that disappear into the Rails gems, a good way to debug them is like so:
begin
// your failing code here
rescue StandardError => e
puts e.backtrace
byebug
puts e.inspect
raise (e)
end
WARNING: do not leave this code in your app or check it in unless you explicitly want to use exception handling for flow control (not recommended!). This is recommended ONLY for debugging purposes.
here you will see the full backtrace to the line number in the Gem where it is failing. when debugging Gems be careful— remember to un-do any changes you make and know that your monkey-patching inside of the Gem code doesn't affect your production code.

Failure/Error: config.include ::Rails::Controller::Testing::TemplateAssertions, type: :controller

I am trying to run a single test file: bundle exec rspec spec/models/user_spec.rb
But get following error:
An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: config.include ::Rails::Controller::Testing::TemplateAssertions, type: :controller
NameError:
uninitialized constant Rails::Controller
Did you mean? ApiController
# ./spec/rails_helper.rb:149:in `block in <top (required)>'
# ./spec/rails_helper.rb:61:in `<top (required)>'
# ./spec/models/user_spec.rb:3:in `require'
# ./spec/models/user_spec.rb:3:in `<top (required)>'
No examples found.
Initially, I was getting following error:
An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: module Shoulda::Matchers::ActiveModel
NameError:
uninitialized constant Shoulda
# ./spec/support/matchers/validate_kept_of_matcher.rb:4:in `<top (required)>'
# ./spec/rails_helper.rb:51:in `block in <top (required)>'
# ./spec/rails_helper.rb:51:in `each'
# ./spec/rails_helper.rb:51:in `<top (required)>'
# ./spec/models/user_spec.rb:3:in `require'
# ./spec/models/user_spec.rb:3:in `<top (required)>'
No examples found.
But then it fixed after I added following to the test file.
require "shoulda/matchers"
I am newbie in Ruby/Rails world, can someone please give a direction?
Rails::Controller::Testing::TemplateAssertions was removed in Rails 5.
You can re-add the depreciated functionality by installing the Rails controller testing gem. However the use of controller specs, assigns and template assertions is discouraged by both the RSpec and Rails teams and is not very future proof.
The community accepted solution is to write request specs and stop poking inside your controllers.

undefined method `users' for Tests

I'm trying to write a simple test but my application is not detecting 'users' keyword when I'm using it in my Test functions:
post_controller_test.rb:
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
test "should fetch facebook post" do
sign_in users(:one)
get(:save_posts_from_facebook_page,{'id'=>'my_id'},{'access_token'=>Rails.application.secrets.fb_access_token})
assert_response :success
end
end
test_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
include Devise::TestHelpers
# Add more helper methods to be used by all tests here...
end
users.yml:
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
email: hello#testing.com
encrypted_password: <%= Devise.bcrypt(User,'password') %>
links:
causes:
two:
email: MyString
password: MyString
links:
causes:
Output of rake/Error:
Run options: --seed 42684
# Running:
E
Finished in 0.010275s, 97.3279 runs/s, 0.0000 assertions/s.
1) Error:
PostsControllerTest#test_should_fetch_facebook_post:
NoMethodError: undefined method `users' for #<PostsControllerTest:0x000001042152f0>
test/controllers/posts_controller_test.rb:9:in `block in <class:PostsControllerTest>'
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
Update:
Note that I'm using MongoId.
When I add 'fixtures :all' to my test_helper.rb, I get:
rake
rake aborted!
NoMethodError: undefined method `fixtures' for ActiveSupport::TestCase:Class
/Users/gautambajaj/My Stuff/FreeFromBorders/f2b_website/test/test_helper.rb:7:in `<class:TestCase>'
/Users/gautambajaj/My Stuff/FreeFromBorders/f2b_website/test/test_helper.rb:5:in `<top (required)>'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/My Stuff/FreeFromBorders/f2b_website/test/controllers/posts_controller_test.rb:1:in `<top (required)>'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
Tasks: TOP => test:run
(See full trace by running task with --trace)
In your test_helper.rb file, you need to add fixtures :all:
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# rest of the codes in this file
end
Once you add that line in your test_helper.rb file as shown above, your test will pass.
Update
As Sergio Tulentsev mentioned in the comment, Mongodb doesn't support transactions, so transactional fixtures can't be used in your case. Take a look at this answer which states that and also this google group discussion as well.
I would recommend you to use factory_girl instead, which supports mongoid and is pretty awesome.

Rails i18n spec doesn't work

In my rails app, I use the gem i18n_spec to add rspec tests that check if my i18n config files are correct.
The spec file
require 'spec_helper.rb'
require 'i18n-spec/tasks'
Dir.glob('config/locales/*.yml') do |locale_file|
describe "#{locale_file}" do
it { subject.should be_parseable }
results in errors
../gems/i18n-spec-0.6.0/lib/i18n-spec/tasks.rb:5:in `<top (required)>': undefined method `namespace' for main:Object (NoMethodError)
from ../gems/backports-3.3.3/lib/backports/tools.rb:328:in `require'
from ../gems/backports-3.3.3/lib/backports/tools.rb:328:in `require_with_backports'
from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:251:in `block in require'
from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:236:in `load_dependency'
from ../gems/activesupport-3.2.21/lib/active_support/dependencies.rb:251:in `require'
from ../spec/selftest/i18n_spec.rb:3:in `<top (required)>'
Why do you want to test if they are parseable? If you are asserting any I18n key in a unit / feature spec Rails will load your I18n files and throw a syntax error if the yml files aren't parseable!
You will get assertions like so:
within(".mod-header .login") { click_link I18n.t('public.menu.login') }
find("#main-container h1").should have_content I18n.t('pages.login.title')
This way Rails will load and parse your yml files. And if there are any errors you will find them in your test output!
And errors like this:
<Psych::SyntaxError: (your_project_path/config/locales/nl.yml): mapping values are not allowed in this context at line <num> column <num>>
or
<Psych::SyntaxError: (your_project_path/config/locales/nl.yml): did not find expected key while parsing a block mapping at <num> column <num>>

How to test a rake task with RSpec?

I have an Ruby 2.1/Rails 3.2 application which uses the asset pipeline. We are also using a fragile (alpha) gem which causes "rake assets:precompile" to fail at times. I would like to write an rspec test which ensures that this rake task always passes before we commit our code.
I wrote a test in spec/asset_precompile_spec.rb which looks like this:
require 'spec_helper'
require 'rake'
describe 'assets:precompile' do
before { MyApp::Application.load_tasks }
it { expect { Rake::Task['assets:precompile'].invoke }.not_to raise_exception }
end
I then ran it on the command line using
rspec spec/lib/assets_precompile_spec.rb
The output I got looked like this:
1) assets:precompile
Failure/Error: it { expect { Rake::Task['assets:precompile'].invoke }.not_to raise_exception }
expected no Exception, got #<RuntimeError: Command failed with status (1): [/home/railsdev/.rvm/rubies/ruby-2.1.2/bin/...]> with backtrace:
# ./spec/lib/assets_precompile_spec.rb:7:in `block (3 levels) in <top (required)>'
# ./spec/lib/assets_precompile_spec.rb:7:in `block (2 levels) in <top (required)>'
# ./spec/lib/assets_precompile_spec.rb:7:in `block (2 levels) in <top (required)>'
Finished in 0.71247 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/lib/assets_precompile_spec.rb:7 # assets:precompile
I have looked far & wide, and I can't find any example to run "rake assets:precompile" which actually works in my RSpec environment. I have tried explicitly loading the spec_helper.rb file, I have tried explicitly requiring "factory_girl", but I cannot find anything which works.
Is there a way to make a test run this rake task run in a RSpec test?
Try Rake::Task['assets:precompile:all'].invoke
instead of Rake::Task['assets:precompile'].invoke
In my case it helped.

Resources