devise helper method, confirm! cannot be used/found within factory setup - ruby-on-rails-3

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.

Related

Rails Mocha gem - NoMethodError: undefined method `any_instance'

I'm using the Mocha gem in a Rails project to make stubs and bypass some live API calls.
I'm conforming to the documentation but keep getting an error.
In Gemfile (I use Bundler) I have:
gem 'minitest'
gem 'minitest-rails'
gem 'mocha', :require => false
In test_helper.rb:
require 'minitest'
require 'mocha/mini_test' # This is the last require statement
In product_controller_test.rb:
require 'test_helper'
class ProductControllerTest < ActionController::TestCase
setup do
Product.any_instance.stubs({
manufacturer: "Acme"
})
end
end
The output is always NoMethodError: undefined method 'any_instance' for #<Class:blah>.
I have another Rails project where I can use any_instance successfully, so I've tried resolving by replicating the gems, require statements, etc. but with no success. I've also tried different permutations like require 'minitest/autorun', require "minitest/rails', and require 'mocha/setup'/require 'mocha', etc.
I'm on Rails 4.1.1 and using MiniTest 5.3.3 and Mocha 1.0.0.

factory girl error

I am getting an error when I'm running my test file.
factory.rb:334:in `factory_by_name': No such factory: user (ArgumentError)
I have the user model but still why am getting this error?
My factories.rb contains:
FactoryGirl.define do
factory :user do |u|
u.sequence(:email) {|n| "#{n}#email.com"}
u.login 'Joe'
u.password 'password'
u.password_confirmation 'password'
u.phone '1111111111'
u.gender 'F'
u.active '1'
u.social_profile_active '1'
end
end
in test_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'factory_girl'
#Factory.find_definitions
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
#fixtures :all
# Add more helper methods to be used by all tests here...
end
my ruby version is 1.8.7 and rails3.0.1
And my gemfile looks like this:
group :test do
gem 'shoulda'
gem 'factory_girl_rails'
gem 'mocha'
end
Rename your directory to one of the following:
Factories can be defined anywhere, but will be automatically loaded if
they are defined in files at the following locations:
test/factories.rb
spec/factories.rb
test/factories/*.rb
spec/factories/*.rb
https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md

rails activeadmin errors rspec

my database is MongoDB and I use ActiveAdmin.
before i add gem 'activeadmin-mongoid', :git => 'git://github.com/simplybusiness/activeadmin-mongoid.git' rspec pass every test with mongoDB, but after that rspec get an errors
ActiveRecord::ConnectionNotEstablished
file config/application.rb I repalce
require all with
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
I removed any activerecord config in folder config . I follow that link
sorry about my bad english.
Any help would be appreciated. Thanks!

rspec returns "PG::Error: ERROR: relation "table_name" does not exist"

Environment is REE(2011.12) on rvm, rspec 2.8.0, rails 3.0.6, and pg 0.13.2. Using PostgreSQL 8.3.17 on CentOS 5.6. The db:migrate have work correctly. But rspec have got following error.
1) ApiController articles OK
Failure/Error: Unable to find matching line from backtrace
ActiveRecord::StatementInvalid:
PG::Error: ERROR: relation "table_name" does not exist
: DELETE FROM "table_name"
I'm updating my project from rails 2.3.5 with rspec 1.x series to rails 3.0 with rspec2. Copied all rspec tests, and I have merged old spec_helper.rb and new one(It was generated rails g rspec:install).
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
end
I read similar question about this error.So I tried rake db:test:prepare or rake db:test:load, But It's not resolve. Do you have any idea?
It looks like the test hasn't run on test database... How do I do? :(
I've run into this in two instances (updated 6/13/2012):
First:
I haven't yet migrated my test database...
rake db:migrate
...which you need to do before both db:test:prepare and db:test:load.
When you invoke rspec it should prepare and load your database for you anyway. You shouldn't have to do that by hand.
Second:
A typo in my migration. I accidentally reversed my table and column names in the parameter list.
A migration on a Rails 3.1 project:
def change
add_column :had_import_errors, :studies, :boolean, :default => false
add_column :import_data_cache, :studies, :text
end
...which is wrong, because has_import_errors and import_data_cache are my column names, and they therefore should come second, not first.
The correct migration, with the table name first was:
def change
add_column :studies, :had_import_errors, :boolean, :default => false
add_column :studies, :import_data_cache, :text
end
This usually happens when you have rspec running while migrating (usually via guard). A simple solution is to quit guard, do the migration and restart guard.
It typically indicates that there is another open PostgreSql connection. To bubble up the right error try % rake db:test:prepare
Running test prepare showed the following below and when the open connection (PGAdmin in my case) was closed the issue was resolved.
rake aborted!
PG::Error: ERROR: database "xyz_test" is being accessed by other users
DETAIL: There are 1 other session(s) using the database.
: DROP DATABASE IF EXISTS "xyz_test"
Tasks: TOP => db:test:load => db:test:purge
(See full trace by running task with --trace)
Just hit this same error, nothing worked but dropping and re-creating the database, this fixed it for me.
rake db:drop db:create db:migrate

uninitialized constant UsersController with devise and rspec

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?