I have configured a new rails 3 app, with devise and rspec. I have followed the devise wiki, and put
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
in spec/spec_helper.rb. Anyways, when I try to run the controllers spec, I have the error :
spec/controllers/users_controller_spec.rb:3:in `<top (required)>': uninitialized constant UsersController (NameError)
Sounds like devise engine classes are not available in loadpath while testing. Any idea ?
UserController might be missing.
If not, can you paste it here?
Related
I am new to RSPEC and am testing the controllers.
Sometimes it happens that the test passes alone like
rspec spec/controllers/controller_name_spec.rb
it passes but when I run :-
rspec spec
the test fails, same test that passed earlier.
What can be the reason, is it related to scalability of rspec or an application specific issue.
As I found same issue with integration tests also.
Rails -v => 3.2.11
Ruby -v => 1.9.2p320
Rspec-rails version => 2.13.0
rspec version => 2.13.0
eg:-
Test case :-
it "should do ##something##" do
plans=Plan.pluck(:id)
plans.delete(#user.subscription.plan.id)
#user.subscription.stripe_customer_token= "cus_1l9m6CicQEXZJ0"
#user.save
get 'apply_change_account', {:subscription=>{:plan_id=>plans.sample}}
flash.now[:success].should_not be_nil
response.should redirect_to dashboard_manage_accounts_path
end
Response error message :-
1) DashboardController Dashboard should do ##something##
Failure/Error: flash.now[:success].should_not be_nil
expected: not nil
got: nil
# ./spec/controllers/dashboard_controller_spec.rb:119:in `block (2 levels) in <top (required)>'
Thanks in advance
Try add flash.clear in before block for every test related to flash.
This may or may not solve the problem, but it's essential to test flash related item to avoid flash object been polluted.
environment.rb:7: uninitialized constant Rails::Initializer (NameError)
from /usr/lib/ruby/gems/1.8/gems/railties-3.1.0/lib/rails/application.rb:78:in `require_environment!'
from /usr/lib/ruby/gems/1.8/gems/railties-3.1.0/lib/rails/commands.rb:39
from script/rails:6:in `require'
from script/rails:6
I'm getting the preceding error, I'm trying to integrate salesforce with a rails app following the PDF here: http://blogs.developerforce.com/developer-relations/2011/02/a-brief-history-of-ruby-rails-with-the-forcecom-platform.html
I have made the instructions required, however it is required to edit the environment.rb
Here is my current version:
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
Newsletter::Application.initialize!
Rails::Initializer.run do |config|
config.gem "asf-soap-adapter", :lib => "activerecord-activesalesforce-adapter"
config.gem "asf-soap-adapter", :lib => 'asf-soap-adapter'
config.database_configuration_file = File.join(RAILS_ROOT, 'config','salesforce.yml')
config.time_zone = 'UTC'
end
I assume based on your tag that you are working in a Rails 3 app, the issue you have is that the environment.rb you have is Rails 2 specific. In Rails 3 you should be specifying your gems in your Gemfile, and removing the Rails::Initializer from your environment.rb.
Add this to your Gemfile:
gem 'asf-soap-adapter', :require =>'activerecord-activesalesforce-adapter'
There are some notes here on the upgrade from Rails 2 -> Rails 3.
http://rails3.community-tracker.com/permalinks/5/notes-from-the-field-upgrading-to-rails-3
I used the new Gem 'databasedotcom' for the same purpose which works perfectly
http://richardvanhook.github.com/databasedotcom-guide/#create_a_databasecom_account
all,
language: Ruby on rails
version: rails 3
gems in question: devise and factory_girl_rails
I refered to https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-(and-rspec) and tried to set up admin and regular devise users using factory girl for my controller specs. I am using the confirmable module in devise.
In spec/support/controller_macros.rb, I call confirm! after getting a user object from the factory.
The code/comment says
> "user.confirm! # or set a confirmed_at inside the factory. Only
> necessary if you are using the confirmable module"
When I set the value of "confirmed_at" in the users factory, spec/factories/users.rb and run my controller specs (rake spec:controllers), it works BUT when I do not set the value of "confirmed_at" and rely on confirm! in spec/support/controller_macros.rb, it seems that confirm! is not found.
I have even tried putting in " include Devise::TestHelpers" in the
spec/support/controller_macros.rb file but still fails.
The error I got was
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `confirm!' for #<User:0x71d9c7901e48>
# ./spec/support/controller_macros.rb:8:in `login_user'
My spec/spec_helper.rb reads as follows.
----------- spec/spec_helper.rb - start ----------------------------
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'webrat'
# 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.mock_with :rspec
config.include Devise::TestHelpers, :type => :controller
config.extend ControllerMacros, :type => :controller
# 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
end
----------- spec/spec_helper.rb - end ----------------------------
What could be wrong that is causing confirm! not to be found/working?
Thank you :)
The confirm! it's just necessary if you are using the :confirmable devise module.
In your user.rb(devise model) put :confirmable in your devise options or if you don't want use this module(this module send a confirmation email to the user.) remove the confirm! from your controller_macros.rb.
I want to have https for entire application.
I decided to use this gem:
https://github.com/tobmatth/rack-ssl-enforcer
However after adding at end
gem 'rack-ssl-enforcer'
in Gemfile and
config.middleware.use Rack::SslEnforcer
in application.rb at end I get error:
/path_to_app/www/.ruby/gems/1.8/gems/railties-3.0.9/lib/rails/application.rb:77:in `send': undefined method `Rack' for #<App1::Application:0xf6dcb314> (NoMethodError)
from /path_to_app/www/.ruby/gems/1.8/gems/railties-3.0.9/lib/rails/application.rb:77:in `method_missing'
from /path_to_app/www/stolikarnia/config/application.rb:57
from /path_to_app/www/.ruby/gems/1.8/gems/railties-3.0.9/lib/rails/commands.rb:21:in `require'
from /path_to_app/www/.ruby/gems/1.8/gems/railties-3.0.9/lib/rails/commands.rb:21
from script/rails:6:in `require'
from script/rails:6
As you see from logs i use Rails 3.0.9. I can't upgrade to RoR 3.1.0 because of dependencies.
In your Gemfile change the requirement to
gem 'rack-ssl-enforcer', :require => 'rack/ssl-enforcer'
Then, in your application.rb pass the class name as String to take advantage of lazy evaluation.
config.middleware.use "Rack::SslEnforcer"
As a side note, I encourage you to check https://github.com/josh/rack-ssl. It's the middleware introduced in Rails 3.1. You can already use it in Rails 3.0 with the additional benefit you won't need to change it once you'll upgrade to Rails 3.1.
ruby-1.9.2-p180 :007 > Factory.define :user do |user|
ruby-1.9.2-p180 :008 > user.email "user#example.com"
ruby-1.9.2-p180 :009?> user.password "foobar"
ruby-1.9.2-p180 :010?> user.password_confirmation "foobar"
ruby-1.9.2-p180 :011?> end
NameError: uninitialized constant Factory
My Gemfile:
group :test do
gem "rspec-rails"
gem 'webrat', '0.7.1'
gem 'spork', '0.9.0.rc4'
gem 'factory_girl_rails'
end
Even tough it seems I have everything as it should, I keep getting that error. I also have factories.rb created.
Thanks
I suppose you try in console in development environment. But you add the Factory gem only in test environment.
If you want access to Factory_girl in development use in your Gemfile :
group :test, :development do
gem 'factory_girl_rails'
end
Or if you want test your Factory launch your console in test environment :
rails c test
We had a similar problem on our end, rake spec seemed to be randomly failing with the error uninitialized constant FactoryGirl. The error was random -> coming and going. We went back half a dozen git commits to try and resolve it. In the end it was a silly mistake.
The fundamental problem is RAILS_ENV is set to development. It needs to be set to test when you run rake spec.
Address by:
Making certain that we are running rake spec in the RAILS_ENV test environment and its exported/sourced properly. To never confuse our environemnts, we modified zsh $RPROMPT env variable to show the current env.
export RPROMPT="[%{$fg_no_bold[yellow]%}$RAILS_ENV%{$reset_color%}]"
Require FactoryGirl in the spec ruby files gave a much better error message. At least rspec would run vs just fail outright this way when the environment was wrong. We also updated our gemfile to make sure factory_girl_rails and factory_girl were loaded both for development and testing.
Now we just run rpsec using the gem guard in a dedicated terminal with the proper RAILS_ENV set.
Its one of those gotchas.
We're running Ruby 1.9.3, Rails 3.2.9, Rspec 2.12, factory_girl 4.1.
I also ran into this while I was doing the Hartl tutorial.
If you are using "Spork" to speed up your tests and it is running in the background when you add the FactoryGirl code, you will need to stop it and restart it.
You should also change Factory.define to FactoryGirl.define, like in this example
FactoryGirl.define do
factory :user do
name 'John Doe'
date_of_birth { 21.years.ago }
end
end
from the factory_girl documentation