Basic TDD in Rails : Configuring Database Cleaner Gem

Objective


Let's handle the requirement #2 we saw in the previous lesson:

If the validation fails we must display the new form with error message and allow user to correct the mistake and resubmit the form.

Steps


Step 1

Before we write a controller spec for the validation failure case, let's run our existing articles controller spec.

$ rspec spec/controllers/articles_controller_spec.rb 

The test that was passing before now failis consistently with the error message:

1) ArticlesController should get all the articles
     Failure/Error: expect(assigns[:articles].size).to eq(1)

       expected: 1
            got: 2

Step 2

To fix this issue, add

  gem 'database_cleaner'

to Gemfile. Run bundle install.

Step 3

Edit the spec/rails_helper.rb as follows:

Add :

require 'database_cleaner' 

to the top of the file and configure database cleaner gem like this:

RSpec.configure do |config|
  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

  # Other code here should be left as it is
end

Step 4

Run the test. The test will now fail because the second spec does not provide value for description. Change the test:

  it 'should get all the articles' do
    Article.create(title: 'test', description: 'test')

    get :index

    expect(assigns[:articles].size).to eq(1)
  end

Run the contoller spec now. It will pass.

If the validation fails we must display the new form with error message and allow user to correct the mistake and resubmit the form.

Step 5

We wrote a feature spec that tested the articles controller create action indirectly. We need to add a controller tests for create and new before we can handle the validation failures.

  it 'should create a new article' do
    expect do
      post :create, { article: { title: 'test', description: 'test' }}
    end.to change{Article.count}.by(1)
  end

This test passes.

Step 6

Let's add the next test to check that it redirects to article index page after creating the article.

  it 'should redirect to articles index page after creating the article' do
    post :create, { article: { title: 'test', description: 'test' }}

    expect(response).to redirect_to articles_path
  end

Run the test. It also passes. Now we can handle the validation failure case :

  it 'should redirect to articles index page after creating the article' do
    post :create, { article: { title: 'test' }}

    expect(response).to render_template(:new)
  end

This test fails.

Step 7

Change the create action:

  def create
    allowed_params = params.require(:article).permit(:title, :description)
    article = Article.new(allowed_params)

    if article.save
      redirect_to articles_path
    else
      render :new
    end
  end

Run the test. It now passes.

Summary


In this lesson we configured database_cleaner gem and implemented the feature where if the validation fails we allow the user to correct the mistake.


Related Articles


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.