I'm using rspec to perform feature tests and I can't save the user in the DB before the log in.
I'm using factory girl to build the object.
fixture are saved in db at the beginning of the test but are not deleted at the end.(maybe because the test fail. I don't know)
So I can not save the user before clicking on logIn and I get this errror message
--
DEPRECATION WARNING: an empty resource was given to Devise::Strategies::DatabaseAuthenticatable#validate. Please ensure the resource is not nil. (called from set_required_vars at app/controllers/application_controller.rb:43)
spec/features/login_to_mainpage_spec.rb (no error are rescued)
require "rails_helper"
feature 'Navigating to homepage' do
let(:user) { create(:user) }
let(:login_page) { MainLoginPage.new }
scenario "login" do
login_page.visit_page.login(user)
sleep(20)
end
end
A simple page object: spec/features/pages_objects/main_login_page.rb
class MainLoginPage
include Capybara::DSL
def login(user)
fill_in 'email', with: user.email
fill_in 'password', with: "password"
click_on 'logIn'
end
def visit_page
visit '/'
self
end
end
my rails_helper
require 'spec_helper'
require 'capybara/rspec'
require 'capybara/poltergeist'
require "selenium-webdriver"
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
Dir[Rails.root.join("spec/features/page_objects/**/*.rb")].each { |f| require f }
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.before :each do
DatabaseCleaner.start
end
config.after :each do
DatabaseCleaner.clean
end
config.infer_spec_type_from_file_location!
config.include Devise::TestHelpers, :type => :controller
end
Capybara.default_driver = :selenium
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :firefox)
end
in spec helper:
require 'simplecov'
require 'factory_girl'
require 'rspec/autorun'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
ENV["RAILS_ENV"] ||= 'test'
RSpec.configure do |config|
include ActionDispatch::TestProcess
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
config.disable_monkey_patching!
if config.files_to_run.one?
config.default_formatter = 'doc'
end
config.profile_examples = 10
config.order = :random
Kernel.srand config.seed
end
EDIT 1
I switch "gem 'factory_girl'" to "gem 'factory_girl_rails'"
and add this to application.rb
config.generators do
|g|
g.test_framework :rspec,
:fixtures => true,
:view_specs => false,
:helper_specs => false,
:routing_specs => false,
:controller_specs => true,
:request_specs => true
g.fixture_replacement :factory_girl, :dir => "spec/factories"
end
I still can not save the user in DB.
Everything pass but I put some sleep(10) in by code to refresh my DB and see te records and user is never saved
EDIT 3
My problem is actually very simple. FactoryGirl.create never save the data in DB if I put in my rails_helper:
config.use_transactional_fixtures = true
or
config.before :each do
DatabaseCleaner.start
end
config.after :each do
DatabaseCleaner.clean
end
/spec/factories
FactoryGirl.define do
factory :user do
email 'pierre#tralala.com'
password 'password'
password_confirmation 'password'
end
/spec/support/factory_girl.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
spec/features/login_to_mainpage_spec.rb
let(:user) { create(:user) }
scenario "login" do
login_page.visit_page.login(create(:user))
sleep(5)
end
User will not be saved because of the config I cited before.
I need to have data reseted between tests.
EDIT 4
If I'm using the console
RAILS_ENV=test rails c
FactoryGirl.create(:user) it is saved in db.
I don't understand why it does not work in my tests.
Try using let! to create your user.
From the rSpec documentation:
Note that let is lazy-evaluated: it is not evaluated until the first time
the method it defines is invoked. You can use let! to force the method's
invocation before each example.
You said that you are using FactoryGirl but I do not see this in your test. To create my Users with FactoryGirl I always do something like this:
FactoryGirl.create(:user, password: 'test', password_confirmation: 'test', name: 'test')
If setup correctly in your Factory you can just write:
FactoryGirl.create(:user)
To solve your problem with the unique fields FactoryGirl provides you sequences:
sequence :email do |n|
"person#{n}#example.com"
end
factory :user do
email { generate(:email }
end
This will start with "person1#example.com" and add 1 to the number each time FactoryGirl.create(:user) is called.
Related
I can't get Capybara to hold a session beyond one page visit within one spec. I am familiar with the concept that Capybara's driver might be using a different database connection, but anything I do (including not using :js => true) doesn't seem to change anything.
I am just trying to visit a page that requires user authentication. It succeeds on first visit. It fails when I visit it a second time. (This of course all works when I manually test in development.) Code:
activity_areas_spec.rb
require "rails_helper"
RSpec.feature "Activity areas", :type => :feature do
user = FactoryGirl.create(:user)
before(:each) do
login_as(user, :scope => :user)
end
after(:each) do
Warden.test_reset!
end
...
feature "displays activities shared", :js => true do
scenario "to public" do
visit area_activities_path
expect(page).to have_content I18n.t('pages.activity.areas.title') #passes
visit area_activities_path
expect(page).to have_content I18n.t('pages.activity.areas.title') #fails -- user is redirected to login page.
end
end
end
#rails_helper.rb
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 'rspec/rails'
require 'spec_helper'
require 'capybara/rails'
require 'devise'
require 'support/controller_macros'
include Warden::Test::Helpers
Warden.test_mode!
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
# Factory girl
config.include FactoryGirl::Syntax::Methods
# Make routes available in specs
config.include Rails.application.routes.url_helpers
# Capybara
config.include Capybara::DSL
# To get Devise test helpers
config.include Devise::TestHelpers, :type => :controller
config.extend ControllerMacros::DeviseHelper, :type => :controller
config.include ControllerMacros::AttributeHelper, :type => :controller
config.use_transactional_fixtures = false
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
# Database cleaner gem configurations
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
Since I was sensitive to the fact that Capy might be using a different database connection, I have tried following DatabaseCleaner's suggested code to the tee in the place of the above DatabaseCleaner commands:
config.before(:suite) do
if config.use_transactional_fixtures?
raise(<<-MSG)
Delete line `config.use_transactional_fixtures = true` from rails_helper.rb
(or set it to false) to prevent uncommitted transactions being used in
JavaScript-dependent specs.
During testing, the app-under-test that the browser driver connects to
uses a different database connection to the database connection used by
the spec. The app's database connection would not be able to access
uncommitted transaction data setup over the spec's database connection.
MSG
end
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, type: :feature) do
driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test
if !driver_shares_db_connection_with_specs
DatabaseCleaner.strategy = :truncation
end
end
config.before(:each) do
DatabaseCleaner.start
end
config.append_after(:each) do
DatabaseCleaner.clean
end
See here.
#spec_helper.rb
require 'factory_girl_rails'
require "capybara/rspec"
require 'sidekiq/testing'
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
I'm confused easily, so much appreciated for any help that is provided in simple terms. And let me know if you need any other files.
As a quick guess your user = FactoryGirl.create(:user) needs to be inside your before block - otherwise it would only be run once when the feature is loaded - and then removed from the database before the first feature is run
Objects are persisting after tests run. I confirmed by doing a PowerUp.all.count test and the count increases by 2 on each run, which a number equal to the objects created for the test on each run. I don't know if I'm misusing FactoryGirl, or if I have a misconfigured spec_helper.
spec/support/factory_girl.rb:
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
spec/rails_helper.rb:
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.infer_base_class_for_anonymous_controllers = false
config.infer_spec_type_from_file_location!
end
spec/spec_helper.rb:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'factory_girl'
RSpec.configure do |config|
config.order = :random
config.before(:all) do
FactoryGirl.reload
end
Kernel.srand config.seed
config.expect_with :rspec do |expectations|
expectations.syntax = :expect
end
config.mock_with :rspec do |mocks|
mocks.syntax = :expect
mocks.verify_partial_doubles = true
end
end
spec/api/power_up_spec.rb:
describe Api::PowerUpsController, :type => :controller do
describe "GET power_ups" do
it "returns all power-ups" do
FactoryGirl.create :power_up, name: "Increase rate of take", description: "You gain points more quickly"
FactoryGirl.create :power_up, name: "Decrease rate of give", description: "You lose points more slowly"
get :index, :format => :json
expect(response.status).to eq 200
body = JSON.parse(response.body)
power_up_names = body.map { |m| m["name"] }
expect(power_up_names).to match_array(["Increase rate of take",
"Decrease rate of give"])
end
end
end
config.use_transactional_fixtures = false
This turns off the default behavior, which is to rollback transactions after each example. When set to false RSpec does not attempt to manage the test database.
You can use database_cleaner to remove the rows created by tests when RSpec is not using transactions. This is often used when writing feature specs.
I am getting a 'Factory not registered' error using Factory Girl in Rails 4. Here is my factory definition for a simple Devise user in spec/factories/user.rb:
FactoryGirl.define do
factory :user do
email 'test#example.com'
password 'foobar'
password_confirmation 'foobar'
end
end
Here is the spec_helper.rb:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'email_spec'
require 'rspec/autorun'
require 'factory_girl'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.include(EmailSpec::Helpers)
config.include(EmailSpec::Matchers)
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.include Devise::TestHelpers, :type => :controller
config.include FactoryGirl::Syntax::Methods
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
And in the gem file:
gem 'factory_girl_rails', :require => false
And here is the test in spec/models/user_spec.rb:
require 'spec_helper'
describe User do
it "has a valid factory" do
FactoryGirl.build(:user).should be_valid
end
end
This fails, saying 'Factory not registered: user'. I've tried moving the Factory definition to spec/factories.rb, as well as using just factory_girl instead of factory_girl_rails, with no luck. Any suggestions would be greatly appreciated.
You're requiring factory_girl instead of factory_girl_rails. Edit the equivalent line in your spec_helper.rb to this:
require 'factory_girl_rails'
My survey#show view is working in production and development but failing in test.
I have:
/app/views/survey/show.html.haml:
#{#survey.current_score} out of #{#survey.available_points}
/spec/features/survey_spec.rb:
visit competitor_survey_path(#competitor, #survey)
#survey.current_score.should == 1.0 # passes! yay!
#survey.available_points.should == 3.0 # passes! yay!
page.should_have_content "0.0 out of 0.0" #passes! huh???
page.should have_content "1.0 out of 3.0" #fails! huh???
How is this even possible? (As I said, it works fine in production and development.) And more to the point, how do I fix it?
I've tried adding #survey.reload to the example and - #survey.reload if Rails.env.test? to the view.
PS, here is my /spec/supports/spec_helper.rb:
require "rubygems"
require "database_cleaner"
require "pry"
require "plymouth"
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
require "rspec/rails"
require 'declarative_authorization/maintenance'
include Authorization::TestHelper
require "capybara/rspec"
require "capybara/rails"
require "capybara/dsl"
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.fail_fast = true
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.before(:each) { ActionMailer::Base.deliveries.clear }
config.use_instantiated_fixtures = false
config.include(Capybara, :type => :integration)
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.strategy = :deletion
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.after do
if example.metadata[:type] == :feature && example.exception.present? && page.current_path.present?
save_and_open_page
end
end
config.include FactoryGirl::Syntax::Methods
end
def login_as(role)
#role = Role.create! :name => role
#virginia = User.create!(
:username => "Virginia",
:password => "password",
:password_confirmation => "password",
:email => "example#example.com")
#assignment = Assignment.create! user_id: #virginia.id, role_id: #role.id
visit login_path
fill_in "user_session_username", :with => #virginia.username
fill_in "user_session_password", :with => #virginia.password
click_on "submit_user_session"
end
rspec doesn't render the views by default. If it is extremely necessary check how you should do it in the documentation: https://www.relishapp.com/rspec/rspec-rails/v/2-2/docs/controller-specs/render-views
Testing devise sign in using capybara. It seems something is wrong as i cannot test sign in using rspec and capybara. Im using factory girl to define user
FactoryGirl.define do
factory :user do
email 'admin#revol-tech.com.np'
password 'bhaktapur'
password_confirmation 'bhaktapur'
admin true
name 'admin'
confirmation_sent_at "#{DateTime.now}"
confirmation_token 'anupsumhikichiki'
confirmed_at "#{DateTime.now}"
username 'username'
end
end
Here is my spec_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__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'database_cleaner'
# FactoryGirl.find_definitions
Capybara.current_driver = :selenium
# 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::TestHelpers, :type => :controller
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
# config.after(:each) { }
config.after(:each) do
DatabaseCleaner.clean
Warden.test_reset!
end
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
# 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
end
And here is my spec
require_relative '../spec_helper.rb'
include Warden::Test::Helpers
Warden.test_mode!
feature "the signin process" do
before :each do
#user_attr = FactoryGirl.attributes_for(:user)
# #user = FactoryGirl (:user)
User.create!(#user_attr)
end
scenario "signs me in" do
# login_as #user, :scope => :user
visit '/'
fill_in 'Login', :with => "admin#revol-tech.com.np"
fill_in 'Password', :with => "bhaktapur"
click_button 'Sign in'
page.should have_content "Signed in successfully"
Warden.test_reset!
end
end
Also my User model is set to confirmable
Instead of adding all the confirmable data points.. should use
#user = FactoryGirl.build(:user)
#user.skip_confirmation!
#user.save!
Then within your scenario
fill_in 'Login', :with => #user.email
fill_in 'Password', :with => #user.password