Basic TDD in Rails : View Article Feature

Objective


  • Learn how to use save_and_open_page method to debug issues in feature tests.

Requirement


When I click on the hyperlink of the article title in the index page, I should view the article title and description.

Steps


Step 1

Add the new feature test:

  it 'can view the article title and description' do
    Article.create(title: 'test', description: 'Body of the article')
    visit articles_path

    click_link 'test'

    expect(page).to have_text('test')      
    expect(page).to have_text('Body of the article')              
  end

Step 2

Run the test: rspec spec/features/manage_articles_spec.rb.

Error is :

 1) Manage Articles can view the article title and description
     Failure/Error: click_link 'test'
     Capybara::ElementNotFound:
       Unable to find link "test"

Step 3

Change the articles/index.html.erb to make the title into a link like this:

<td><%= link_to article.title, article %></td>

Step 4

The test fails with the same error.

Step 5

Add save_and_open_page to see what's going on like this:

  it 'can view the article title and description' do
    Article.create(title: 'test', description: 'Body of the article')
    visit articles_path

    save_and_open_page

    click_link 'test'

    expect(page).to have_text('test')      
    expect(page).to have_text('Body of the article')              
  end

Step 6

Run the test. We get the message:

Please install the launchy gem to open the file automatically.

when the test fails.

Step 7

Add launchy gem to Gemfile in the test group. Run bundle. Run the test.

Step 8

We see that the link is 'Test Article' instead of 'test'. The record which was created in a previous test does not get deleted before we run a new test. Change the transactional_fixtures flag to false in rails_helper.rb:

config.use_transactional_fixtures = false

Step 9

Create the spec/support directory and move these lines code :

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
end

to spec/support/database_cleaner.rb file.

Step 10

Before trying this suggestion. Check the configuration in rails_helper.rb to make sure, this line:

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

is uncommented. If so and the problem still persists, proceed with following:

The database still is not cleaned up after a test runs. Add after method to the manage_articles_spec.rb:

after(:each) do
  Article.destroy_all  
end

Step 11

Run the test. The test now fails with :

1) Manage Articles can view the article title and description
     Failure/Error: Unable to find matching line from backtrace
     AbstractController::ActionNotFound:
       The action 'show' could not be found for ArticlesController

Step 12

Add the show action to articles_controller.rb:

def show
end

Step 13

Remove the save_and_open_page in the test and run the test.

We get :

1) Manage Articles can view the article title and description
     Failure/Error: Unable to find matching line from backtrace
     ActionView::MissingTemplate:
       Missing template articles/show, application/show with {:locale=>[:en], :formats=>[:html, :xml], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
         * "/Users/zepho/projects/tdd/basics/blog/app/views"

Step 14

Change the show action as follows:

  def show
    @article = Article.find(params[:id])
  end

Step 15

Add the articles/show.html.erb:

<p>
  <%= @article.title %><br>
</p>

<p>
  <%= @article.description %><br>
</p>

Run the test. All tests now pass.

Summary


In this lesson you learned how to use saveandopen_page method to troubleshoot problems when feature test fails for unknown reason. We implemented the view an article feature.


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.