Basic TDD in Rails : Delete an Article Feature


To implement delete an article feature.


Step 1

We are going to write a feature spec, to check for the 'delete' link in the view.

  it 'has delete link' do
    Article.create(title: 'test', description: 'Body of the article')
    visit articles_path

    expect(page).to have_text('Delete')

Step 2

Run the test.

  1) Manage Articles has delete link
     Failure/Error: expect(page).to have_text('Delete')
       expected to find text "Delete" in "Listing Articles Title Description test Body of the article Edit New Article"

Step 3

Add the delete link as follows:

<%= link_to 'Delete', 
             :method => :delete, 
             :confirm => 'Are you sure?' %>

So the articles/index.html.erb file now looks like this:

<h1>Listing Articles</h1>

<table >
    <% @articles.each do |article| %>
        <td><%= link_to article.title, article %></td>
        <td><%= article.description %></td>
          <%= link_to 'Edit', edit_article_path(article) %>
          <%= link_to 'Delete', 
                                             :method => :delete, 
                                             :confirm => 'Are you sure?' %>
    <% end %>

<%= link_to 'New Article', new_article_path %>

Step 4

The test passes.

Step 5

Now replace the it 'has delete link' test to:

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

    click_link 'Delete'

    expect(page).to have_text('Delete success')

Step 6

Run the test. It fails with:

An error occurred in an after hook
    AbstractController::ActionNotFound: The action 'destroy' could not be found for ArticlesController

Step 7

Add the destroy action:

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

Step 8

Run the test again. It fails with:

An error occurred in an after hook
    ActionView::MissingTemplate: Missing template articles/destroy, application/destroy with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
  * "/Users/zepho/projects/tdd/basics/blog/app/views"

Step 9

Let's redirect the user to the articles index page.

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

    redirect_to articles_path, notice: 'Delete success'

Step 10

Run the test again. All tests pass now.


Write a test for destroy action in articles controller to check that the destroy action redirects the user to articles index page.


In this lesson, we started with a simpler test to implement deleting an article feature, we revised the same test for deleting an article.

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.