Create an account or log in

Keep learning for free! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

2 Días
8 Hrs
41 Min
37 Seg

Configurar rspec + factorybot

5/33
Resources

How to configure gems in a Rails project?

Properly configuring gems in a Rails project is crucial to ensure that they integrate smoothly and to guarantee the proper functioning of your application. Follow these steps to make the necessary configurations with confidence and style.

How to install and configure RSpect Rails?

RSpect Rails is an essential tool for testing Rails projects. Follow these steps to install and configure it:

  1. Installing the gem: We have already added the RSpec Rails gem to the development and test group in our Gemfile and run bundle install.

  2. File generation: Run the following command in the terminal to generate the necessary files:

    rails generate rspec:install
  3. Test execution: Use bundle exec rspec to run the project tests. If there are no tests configured, the command will finish successfully indicating zero examples and zero failures.

How to integrate Shoulda Matchers?

Shoulda Matchers allows you to write tests in a more readable and efficient way. Here's how to set it up:

  1. Configure Shoulda Matchers: include the suggested configuration in rails_helper.rb after the require line 'rspec/rails'. This is critical to ensure that Rails is fully loaded before adding additional requirements.

  2. Remove unnecessary lines: If you are not using mini_test or test_unit, remove these lines from rails_helper.rb. Keep the lines that involve Active Record, ActiveModel, Action Controller and other essential Rails elements, as they are used for integration with the database and HTTP drivers.

  3. Verification: After performing these configurations, run bundle exec rspec again to verify that everything is working correctly.

How to configure Database Cleaner?

Database management during testing is key to avoid leaving data residues. Here we show you how to integrate and configure Database Cleaner:

  1. Add the configuration to RSpec: Copy and paste the suggested configuration into the RSpec configuration block inside rails_helper.rb. Make sure it is inside the appropriate configuration block.

  2. Configure strategies:

    • Before each test suite: Use the transaction strategy and clean up the database by truncating it.
    • Around each test: Use the specified cleanup strategy to ensure that no data remains after each test.

How to implement Factory Bot?

Factory Bot is an essential tool for creating test objects. To configure this gem, follow these steps:

  1. Configuration in RSpec: Add the Factory Bot configuration inside the RSpec configuration block in rails_helper.rb. This allows the use of Factory Bot methods without manually importing the library in each test file.

  2. Final verification: Run bundle exec rspec to make sure that all configurations were performed correctly.

By following these instructions, you will have successfully configured RSpec Rails, Shoulda Matchers, Database Cleaner and Factory Bot in your Rails project, optimizing your tests and ensuring a cleaner and more efficient workflow. Remember that these configuration steps only need to be done once at the beginning of the project - now you're ready to move on to the next step in your Rails project!

Contributions 11

Questions 1

Sort by:

Want to see more contributions, questions and answers from the community?

Agrega en rails_helper.rb

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    # Choose a test framework:
    with.test_framework :rspec

    # Choose one or more libraries:
    with.library :active_record
    with.library :active_model
    with.library :action_controller
    # Or, choose all of the above:
    with.library :rails
  end
end

Es un gran alivio tener en el curso clases como ésta, donde además de mostrarnos cómo configurar las gemas, el profesor explica qué es lo que hace ese código adicional que agregamos para la configuración.

Cuando se aprende una nueva tecnología se desconocen muchas cosas, como éste tipo de detalles.

Es posible llegar a perder muchas horas por errores generados por una mala configuración.

2020

group :test do
  gem 'factory_bot_rails', '~> 4.0'
  gem 'shoulda', '~> 3.6.0'
  gem 'shoulda-matchers', '~> 3.1.3'
  gem 'rails-controller-testing', '~> 1.0.4'
  gem 'faker', '~> 1.9'
  gem 'database_cleaner', '~> 1.7'
  gem 'rspec-rails', '~> 3.5'
end

Holas!!!

Hay una actualizacion amigos asi que no se coloca nada en rails_helper

no explican nada, solo habla, habla y habla

Tambien se pueden correr las pruebas ejecutando solamente el statement rspec

Viejo que cursos mas de mierda jaja

En la documentación de shoulda-matchers aparece, en la configuración, estas líneas:

Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end

Entiendo que con “with.library :rails” incluye active_record, etc.

importante

En 2023, no debes usar directamente la gema “database_cleaner”, debes usar la que incluye los drivers que más se adecuen a tu proyecto. En este caso, debemos instalar la gema "database_cleaner-active_record’
Más información

Configuración de factory_bot

Cuando se habla de la configuración de “FactoryBot” en el minuto 6:25, lo que se esta diciendo es que no hay que configurarla (explícitamente) sino que la gema que instalamos “factory_bot_rails” hereda todos los metodos de factory_bot ?

Genial la explicación previa al desarrollo