Basic TDD in Rails : Delete an Article Feature

Objective


To implement delete an article feature.

Steps


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')
  end

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', 
             article_path(article), 
             :method => :delete, 
             :confirm => 'Are you sure?' %>

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

<h1>Listing Articles</h1>

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

<%= 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')
  end

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])
    @article.destroy
  end

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])
    @article.destroy

    redirect_to articles_path, notice: 'Delete success'
  end

Step 10

Run the test again. All tests pass now.

Exercise


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

Summary


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