How to access Clearance Shoulda macros in RSpec - ruby-on-rails

I can't work out how to get access to the shoulda macros (sign_in_as, etc) in my RSpec tests. I've installed and unpacked the shoulda gem into vendor and I've run the generator to install clearance. I suspect I need to use the right require statement in spec_helper.rb but nothing I've been able to find works for me. I've completely run out of ideas.

I got it to work by following the instructions on this blog post: http://blog.smajnr.net/2011/03/clearance-rspec-shoulda.html
# in spec_helper.rb
require 'clearance/shoulda_macros'
RSpec.configure do |config|
# ...
# Include Shoulda macros for Clearance
config.include Clearance::Shoulda::Helpers
end

Add the following to to the top of your spec_helper.rb,
require 'clearance/shoulda_macros'
And then inside the Rspec configure block add the following,
config.extend(Clearance::Shoulda::Helpers)

Related

Why Rspec cannot find the select2 method?

I've just joined a new company and when I'm using the select2 method in Rspec, I get the following error: NoMethodError:undefined method `select2' for...
We use the 'capybara-select-2' gem.
So I added in spec_helper.rb the following config: config.include CapybaraSelect2 and it's perfectly works. Problem, when tests are pushed on heroku, it raises an error: uninitialized constant CapybaraSelect2.
The weird thing is my colleague doesn't need to add the config line for using this method...
Do you have any idea why he can use it without it and I can't? Thanks for your help.
Your coworker probably did gem install capybara-select-2 at some point installing it globally into his environment, while you're using it via bundler. You should make sure it has been included into the :test group in your gemfile.
That being said I wouldn't recommend using that gem anymore, it hasn't been updated in years.
My CTO finally found a way to make my code working:
I was calling config.include CapybaraSelect2 at the end of spec_helper.rb so he moved it within rails_helper.rb, just as following:
config.include Devise::Test::IntegrationHelpers
config.include ActiveSupport::Testing::TimeHelpers
config.include ::Passwordless::TestHelpers::SystemTestCase
config.include CapybaraSelect2
Everything works fine now!

Is possible to test before Rails loaded?

I'm creating a gem and this gem depends Rails is loaded. I need to create a scenario when Rails is not loaded. I have this code in my gem as an example:
if defined?(Rails) && Rails.application
#some code
end
I've tried to create a stub, but it seems Rails restarts the application method when we set it with nil. https://github.com/rails/rails/blob/fbe2433be6e052a1acac63c7faf287c52ed3c5ba/railties/lib/rails.rb#L38
Rails.stub(:application, nil) do
# test
end
Easy. Just setup a bootstapper file for your test that does not load Rails.
# spec/barebones_helper.rb
RSpec.configure do |config|
end
# optionally load your dependencies:
# Bundler.require(:some_group)
# spec/lib/mygem_spec.rb
require 'barebones_helper'
In fact rspec-rails already generates two separate files*:
spec_helper.rb - just configures rspec.
rails_helper.rb requires spec_helper.rb and then boots Rails through require File.expand_path('../config/environment', __dir__).
Simply changing require 'rails_helper' to require 'spec_helper' will change the test to become executed outside rails.
However be aware that running the tests through Spring will generate strange results as it will keep Rails running in the background. If you're using a spring binstub you can disable spring with the DISABLE_SPRING env var.

Guard + Zeus + Rspec-Rails: undefined method 'configure' for Rspec:Module

I'm using the following:
Rails 4.1.1
guard-zeus 2.0.0
rspec-rails 3.0.1
Out of box default rails g rspec:install and guard init
When I run guard and save a spec file, I get the error:
undefined method `configure` for RSpec:Module (NoMethodError)
I can run specs with rspec spec and rake just fine.
In spec_helper, if I require 'rspec/rails before the configure block,
guard works fine, but then rspec spec fails with the error:
uninitialized constant ActiveSupport::Autoload (NameError)
I'm guessing there's a problem with load order now that rails_helper and spec_helper
are separated.
Two questions:
How can I fix this?
Is there a different solution for continuous integration locally that you can recommend that works with latest Rails and Rspec.
You only have to answer one question.
The following fix worked for me:
#spec/spec_helper.rb
require 'rspec/core'
Throwing out a quick answer that may be the problem. Your spec_helper file should have the following order:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
rspec/rails needs to be required after the config/environment require.
The following:
undefined method `configure` for RSpec:Module (NoMethodError)
suggests you are are missing a
require 'rspec'
This normally isn't necessary, but if you put it in your spec/spec_helper.rb that should work.
(If you run RSpec directly, it's included already with RSpec).
The reason it's not included is perhaps:
you are not running guard through bundler
or your Gemfile does not have:
gem 'rspec' # without the require: false
or something may be wrong with your .rspec file (which should be present)
The require 'rspec/rails' should probably go into the spec/rails_helper.rb...
... but a better way would be to update your rspec-rails gem and run:
rails generate rspec:install
and if you're prompted - used 'd' to differences (and ideally use the recommended changes).
You should add following require to top of file spec_helper.rb
require 'rspec/rails'
Take the reference here: Zeus GitHub issue 308

Uninitialized constant Capybara

I'm having a problem running a test in rails with Capybara.
Whenever I run it, it tells me I have an 'uninitialized constant Capybara (NameError)' in my spec_helper.rb file.
I'm following this tutorial:
http://www.railstutorial.org/book/static_pages#code-capybara_dsl
This is my spec_helper.rb
RSpec.configure do |config|
config.include(Capybara::DSL)
end
and I'm trying to run this test static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
If there is any other more information needed just let me know
--------UPDATE----------------
I figured out the problem. The version of Rspec I have creates a rails_helper.rb file in the spec folder. I had to do:
require 'capybara/rspec'
in that file and config.include Capybara::DSL in the configurations.
-----New Problem------------
But now I get another problem it is saying that 'visit' is undefined.
undefined method `visit' for #<RSpec::ExampleGroups::StaticPages::AboutPage:0x000001033f5d50>
I tried require 'capybara', and require 'capybara/dsl' but they all still give me errors and then some.
I sort of found out the problem, I'm getting initialized constant capybara because in newer versions of rspec they have a new folder "rails_helper" when you first create a spec folder it creates both rails_helper and spec_helper. The first mock speck test in the spec folder has this at the top
require 'rails_helper'
I was following a tutorial that told me to require 'spec_helper' but that is not true for newer versions of rspec.
So I added
config.include(Capybara::DSL)
in the rails helper folder and everything worked, except I get an error with the css but I believe this is because the tests run headless so they do not work. The spec tests run fine after all the errors are displayed though. If you have any more questions just let me know.
It's easy to do everything required and then forget to load the Capybara gem via Gemfile. I've done this so this message looked familiar to me.
Perhaps demonstration of this case is helpful to folks; saving some valuable time. :)
In a previously working environment, I hide Capybara by commenting out the capybara line in Gemfile. The result is a similar error message, 'uninitialized constant Capybara (NameError)', which is thrown by rails_helper:
$ rspec spec
Running via Spring preloader in process 17470
/Users/Art/RailsProjects/MyRailsProj/spec/rails_helper.rb:67:in `block in <top (required)>': uninitialized constant Capybara (NameError)
Incidentally, the O.P. may have been using a version of Capybara that preceded the release where two files exist: rails_helper and spec_helper.
In my setup,
config.include(Capybara::DSL)
is in rails_helper.rb.
BTW, if you see examples of the Capybara include in spec_helper, then those pages may be describing the previous file organization.
Add require 'capybara/rails' to your test helper

Testing Rails 3.1 mountable engine with Rspec

I started making a Rails 3.1 engine, and I'm having a hard time testing it using rspec.
First of all, if I run rails g integration_test whatever it creates a regular integration test in tests/integration instead of spec/requests (the rspec-rails gem is installed and required as a development dependency in the gemspec file)
Also, when I run a spec test I get an error saying the table corresponding to the model I'm testing has not been created. I tried rake engine_name:install:migrations and running rake db:migrate from inside the dummy app, and I get a "table already exists" error.
Everything just seems disconnected, I feel I'm missing something here to make the rspec gem work seamlessly as it usually does with full rails applications.
I followed all the changes from here http://rubyx.com/2011/03/01/start-your-engines and I can test the engine manually by launching the dummy app via the console as shown here http://railscasts.com/episodes/277-mountable-engines.
Is there a way to make rspec the default for testing a rails 3.1 engine?
I am using RSpec with a Rails engine without issues.
I created my plugin using the following switches: -T --full --dummy-path=spec/dummy.
-T excludes test/unit
--full indicates that the plugin is an engine
--dummy-path is simply so that we don't get a test directory (the
default is test/dummy).
From there I used the spec_helper from the "start your engines" article:
# Configure Rails Envinronment
ENV["RAILS_ENV"] = "test"
require File.expand_path("../dummy/config/environment.rb", __FILE__)
require 'rspec/rails'
ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[File.join(ENGINE_RAILS_ROOT, "spec/support/**/*.rb")].each {|f| require f }
RSpec.configure do |config|
config.use_transactional_fixtures = true
end
For the generators. I add a config.generators block to my engine.rb file like so:
module MyEngine
class Engine < Rails::Engine
config.generators do |g|
g.test_framework :rspec, :view_specs => false
end
end
end
With that, I'm able to get rspec tests when running a generator like the model generator.
As for the DB, is your database.yml file set up correctly? Did you load the test environment, e.g. rake db:test:clone or rake db:migrate RAILS_ENV=test? My guess is that RSpec can't see your tables because there isn't a test database set up.
I was looking for the same answer and I found the combustion gem* which promise to setup a full environment for spec'ing your engine in a simpler way. Just add
gem.add_development_dependency 'combustion', '~> 0.3.1'
to your gemspec and run
bundle exec combust
to reproduce a full rails app in your spec directory.
*I haven't tried it yet...

Resources