So, you’re asked to test your application but have no idea how to begin. Or you read about TDD, RSpec, FactoryBot, Capybara, and maybe you didn’t understand what they were meant for, or maybe when you tried to run them into your project nothing worked.

This is a beginner guide on how to set up your environment for using these technologies. If you are not familiar with these terms, here is a brief explanation about them:

  • TDD (Test Driven Development): It is a development technique where the tests are built before starting the real code. In this process, the tests are created based on the requirements, and the code is implemented to pass the tests.
  • RSpec: It’s a Ruby written language for testing Ruby code. Yeah! This is your testing tool. A language created with Ruby in order to offer an interface to write readable tests for your project. It is an alternative for Minitest, the default Rails testing framework. This is a test example for a User model with RSpec.
RSpec.describe User, type: :model do it 'name should not have less than 3 characters' do user = User.new user.name = 'ab' user.valid? expect(user.errors[:name]).to include('is too short (minimum is 3 characters)') end end
  • Capybara: Another testing tool - a library that enables browser interaction using Ruby. In other words, this one is used to simulate how a user interacts with your web application. It is combined with RSpec to create the ‘feature testing’ environment. Here is an example of testing with RSpec and Capybara for a ‘login’ page:
RSpec.feature 'Users', type: :feature do it 'are redirected to root page when login is sucessfull' do fill_in 'session_username', with: 'myusername' fill_in 'session_password', with: 'mypassword' find("input[type='submit']").click expect(page).to have_current_path(root_path) end end
  • DatabaseCleaner: As the name says, it is a tool used to clean the database after tests. It is useful for situations where running a test creates a new object in the database that could interfere with other tests.
  • FactoryBot: It allows us to create an object or a collection of objects with predefined sets of values for our tests. What does it mean? If we have a ‘User’ model, we can create predefined values for different kinds of users. That means it is possible to define a ‘random user’ and a ‘regular user’ factory and use them inside your tests, without the need to declare every time. This is a really simple example of how it can be declared used inside our tests:
FactoryBot.define do factory :user do name { 'Simple User' } factory :user_random do sequence(:name) { |n| "User #{n}" } end end end
RSpec.describe User, type: :model do it '...' do user = create(:user) ## user.name: 'Simple User' random_user = create(:user_random) ## random_user.name: 'User 1' random_user = create(:user_random) ## random_user.name: 'User 2' end end

Ok, now that you are already familiar with these terms we can move on and configure your project to use these technologies.

#rails #rspec #factory-bot #capybara #testing #ruby-on-rails #environment-setup #coding

How To Configure Rails Tests with RSpec, FactoryBot and Capybara
1.50 GEEK