Failure / Error: expect (page) .to have_content Capybara does not check page content

0

Hi, I created my test with capybara and rspec. This is my test

require 'rails_helper'

RSpec.feature "Users", type: :feature do
  context "crear nuevo usuario" do
    scenario "creacion exitosa" do
      user= FactoryBot.create(:user, :super_usuario)
      login_as(user, :scope => :user)
      visit new_user_path
      within('#frm_user') do 
       fill_in('user_user', with: 'elusuario')
       fill_in('user_name', with: 'Jhon Smith')
       fill_in('user_email', with: '[email protected]')
       select('Cliente', from: 'user[roles]')
       fill_in('user_password', with: '123456')
      end
      click_button 'Grabar'
      expect(page).to have_content('Usuario creado satisfactoriamente.')  
    end  
  end

  context "editar usuario" do 
  end

  context "eliminar usuario" do 
  end  
end

And this is the method in my controller

 POST /users
  # POST /users.json
  def create
    @flag_nuevo_usuario=true
    @user = User.new(user_params)

    authorize @user

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'Usuario creado satisfactoriamente.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

But when I run it just throws me an error that fails, but it does not tell me what value it is returning. I am using pundit with devise and to validate access to tests I have configured in my rails_helper.rb

#Para que se pueda autenticar por capybara para pruebas
 config.include Warden::Test::Helpers

This is the error trace:

Users crear nuevo usuario creacion exitosa
 Failure/Error: expect(page).to have_content('Usuario creado satisfactoriamente.')
   expected to find text "Usuario creado satisfactoriamente." in "Dashboard Charts Tables Dato Maestros Navbar Cards Example Pages Login Page Registration Page Forgot Password Page Blank Page Reportes Second Level Item Second Level Item Second Level Item Third Level Third Level Item Third Level Item Third Level Item Messages 12 New New Messages: David Miller 11:21 AM Hey there! This new version of SB Admin is pretty awesome! These messages clip off when they reach the end of the box so they don't overflow over to the sides! Jane Smith 11:21 AM I was wondering if you could meet for an appointment at 3:00 instead of 4:00. Thanks! John Doe 11:21 AM I've sent the final files over to you for review. When you're able to sign off of them let me know and we can discuss distribution. View all messages Alerts 6 New New Alerts: Status Update 11:21 AM This is an automated server response message. All systems are online. Status Update 11:21 AM This is an automated server response message. All systems are online. Status Update 11:21 AM This is an automated server response message. All systems are online. View all alerts Logout Inicio/users NUEVO USUARIO /users 1 error impiden que se pueda guardar el registro: User has already been taken × User Name Email Roles Supervisor Contador Cliente Gerente general Gerente operaciones Password Grabar Atras Copyright © Orsis 2018 Ready to Leave? × Select \"Logout\" below if you are ready to end your current session. Cancel Logout"
    
asked by HalleyRios 03.04.2018 в 17:23
source

1 answer

0

I had to correct several things:

1) The clickybutton of capybara was inside the block within ("# frm_user"). I took it out and put it like this:

      within('#frm_user') do 
       fill_in('user_user', with: 'elusuario')
       fill_in('user_name', with: 'Jhon Smith')
       fill_in('user_email', with: '[email protected]')
       select('Cliente', from: 'user[roles]')
       fill_in('user_password', with: '123456')
      end
      click_button 'Grabar'
      expect(page).to have_content('Jhon Smith')  

2) inside the spec / support / .. folder the database_cleaner.rb file  and place the following.

require 'capybara/rspec'

#...

RSpec.configure do |config|

  config.use_transactional_fixtures = false

  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
    # :rack_test driver's Rack app under test shares database connection
    # with the specs, so continue to use transaction strategy for speed.
    driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test

    unless driver_shares_db_connection_with_specs
      # Driver is probably for an external browser with an app
      # under test that does *not* share a database connection with the
      # specs, so use truncation strategy.
      DatabaseCleaner.strategy = :truncation
    end
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.append_after(:each) do
    DatabaseCleaner.clean
  end

end

3) As indicated by the database_cleaner.rb file, I have to deactivate the following line in spec / rails_helper.rb by setting it to false

config.use_transactional_fixtures = false

Finally in my factoryBoot (factoryGirl before)

I put the user {} so that it would not be repeated

FactoryBot.define do
  factory :user do
    user {Faker::Code.asin}
    name "Alex Fin"
    email '[email protected]'
    password 'password'

    trait :super_usuario do
      roles :super_usuario 
    end

    trait :cliente do
      roles :cliente 
    end


  end
end
    
answered by 03.04.2018 / 18:31
source