All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is the difference between spec_helper.rb and rails_helper.rb in RSpec?

Post cover: What is the difference between spec_helper.rb and rails_helper.rb in RSpec?
This content has been automatically translated from Ukrainian.
If you're developing an app with Ruby on Rails, you're probably already familiar with RSpec testing. But every time you open the spec_helper.rb and rails_helper.rb files, you can think: why are there two of them and how are they different?

spec_helper.rb: base/ground for testing

This file is the basic configuration for RSpec. Its task — is to provide minimal settings for tests. Ideally, spec_helper.rb should not depend on Rails. If simple - this is a file that contains basic settings for RSpec, namely:
  1. Visualization of tests: you can ask how the test results will look in the console (for example, progress bar).
  2. Connecting additional utilities: special libraries such as Faker, FactoryBot or custom test helpers can be imported into the file.
  3. Debugging: enable detailed log mode or integrate with pry for debugging during testing.
This file is useful if you need to test individual Ruby classes or methods that independent of Rails. Use only spec_helper.rb allows you to run tests faster because Rails does not boot.

rails_helper.rb: integration with Rails

rails_helper.rb based on spec_helper.rb, but adds Rails-specific settings. Among them:
  • automatic Rails loading;
  • integration with ActiveRecord to work with the database in tests;
  • connection of auxiliary modules for testing controllers, models and pins.
This file is often used for tests that depend on Rails functionality. For example, testing models with validations or controllers with database queries requires loading rails_helper.rb.

What and when to connect?

  • Use spec_helper.rbwhen you test logic that is not related to Rails. These can be regular Ruby classes, PORO or auxiliary modules.
  • Use rails_helper.rbwhen your tests depend on the Rails environment.
In large projects, it is important to minimize the load on the test process (speed up tests on CI, and reduce the check for the use of resources). If you always use rails_helper.rb, even for simple tests, the test run time increases. On the other hand, too frequent separation can make adjustment difficult. Therefore, it is worth combining flexibility and performance, choosing carefully between these files depending on the tasks.

This post doesn't have any additions from the author yet.

What is RDBMS (relational database management system)?
21 Sep 08:56

What is RDBMS (relational database management system)?

meme code
meme code@memecode
 What does the ACID principle mean in programming?
24 Sep 20:31

What does the ACID principle mean in programming?

meme code
meme code@memecode
What is Sonvention over Sonfiguration (in the context of RoR and beyond)?
26 Sep 07:47

What is Sonvention over Sonfiguration (in the context of RoR and beyond)?

meme code
meme code@memecode
Округлення десяткових чисел: математичні правила, JavaScript і Ruby
18 Oct 12:59

Округлення десяткових чисел: математичні правила, JavaScript і Ruby

meme code
meme code@memecode
[Фікс] Factory not registered: "user" (KeyError) (FactoryBot / Rails 7)
07 Dec 10:46

[Фікс] Factory not registered: "user" (KeyError) (FactoryBot / Rails 7)

meme code
meme code@memecode
Що таке PORO в Ruby?
08 Dec 12:46

Що таке PORO в Ruby?

meme code
meme code@memecode
09 Dec 14:50

[Фікс] No such file or directory @ rb_sysopen - tmp/pids/server.pid

meme code
meme code@memecode
What you need to know to build a successful freelance career in IT
23 Dec 16:03

What you need to know to build a successful freelance career in IT

meme code
meme code@memecode
Віртуальний хмарний сервер: що це таке та в чому його особливість
03 Jan 10:58

Віртуальний хмарний сервер: що це таке та в чому його особливість

meme code
meme code@memecode
[Fix] Heroku / SearchBox addon - помилка ідексації "The client is unable to verify that the server is Elasticsearch"
31 Jan 13:09

[Fix] Heroku / SearchBox addon - помилка ідексації "The client is unable to verify that the server is Elasticsearch"

meme code
meme code@memecode
06 Feb 15:31

Фікс помилки [DEPRECATION] #adapters is deprecated. Use #profiles instead. (Codecov / docile)

meme code
meme code@memecode
Що таке Promise у JavaScript і як швидко зрозуміти суть?
18 Feb 11:01

Що таке Promise у JavaScript і як швидко зрозуміти суть?

meme code
meme code@memecode