Updating Integration Tests
This guide walks through how to fix the integration tests for the post pages so the tests pass and follow BDD best practices.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

To continue from where we left off in the last video, open post_rspec.rb in spec/features.

We had error in line #86, which is test case "will have a user associated with it." This test case is failing because it should. A bigger problem is line #78, which is test case "can be created from new form page." This test should fail and it's not failing. So, this is what we need to fix.

If you're wondering why rspec threw an error, it is because there is no value in rationale as nothing was created.

Start the server and open your browser. I'll show you the problem.

If you log in and try to create a new post, you get a growl notification "Overtime request must be greater than 0.0".

large

This means the post is not being created. So, why is the creation test passing then? If you look at the code, you can see that we are checking for a specific content. When I created this case, I wanted it to look for this content in the show page, but it is finding it in the form itself and passing the test.

An easy fix for this is to change the expectation.

it 'can be created from new form page' do
  fill_in 'post[date]', with: Date.today
  fill_in 'post[rationale]', with: "Some rationale"

  expect { click_on "Save" }.to change(Post, :count).by(1)
end

Now, we are not checking for any specific content, rather we are checking in the database to see if the number of posts has increased by 1.

If you run rspec, it throws two failures.

large

Let's fix it. We need a field for overtime_request in both the test cases.

it 'can be created from new form page' do
  fill_in 'post[date]', with: Date.today
  fill_in 'post[rationale]', with: "Some rationale"
  fill_in 'post[overtime_request]', with: 4.5

  expect { click_on "Save" }.to change(Post, :count).by(1)
end

it 'will have a user associated it' do
  fill_in 'post[date]', with: Date.today
  fill_in 'post[rationale]', with: "User Association"
  fill_in 'post[overtime_request]', with: 4.5
  click_on "Save"

  expect(User.last.posts.last.rationale).to eq("User Association")
end

Now, the error is that rspec cannot find overtime_request, and that's right, because we haven't put it in yet.

Open _form.html.erband add the overtime request.

<!-- app/views/posts/_form.html.erb -->

<div class="form-group">
  <%= f.label :overtime_request, class: "col-sm-2 control-label" %>  
  <%= f.number_field :overtime_request, class: "form-control" %>  
</div>

<div class="form-group">
  <%= f.label :date, class: "col-sm-2 control-label" %> 
  <%= f.date_field :date, class: "form-control" %>  
</div>

<div class="form-group">
  <%= f.label :rationale, class: "col-sm-2 control-label" %>    
  <%= f.text_area :rationale, class: "form-control" %>  
</div>

This should get all test cases passing.

But, it doesn't and I know why. Open posts_controller.rb and add overtime_request to the post_params method.

# app/controllers/posts_controller.rb

def post_params
  params.require(:post).permit(:date, :rationale, :status, :overtime_request)
end

Though the error message was not explicit, I just remembered we need to add it, and this comes with experience.

Now, all tests pass and we can check this functionality in the browser.

When you go to the create page, enter the values and click on save, you get this message:

large

This is a HTML validation. We can't have a number field because it takes only whole numbers as values and not decimals. So, we need to change it to a text field.

<!-- app/views/posts/_form.html.erb -->

<div class="form-group">
  <%= f.label :overtime_request, class: "col-sm-2 control-label" %>  
  <%= f.text_field :overtime_request, class: "form-control" %>  
</div>

If you refresh the browser and try it again, everything should work.

If you look at the database, the value for overtime_request will be stored as a BigDecimal.

I forgot one thing here, we have to run bundle exec rake db:setup which we didn't do earlier when we updated our seeds.rb file. Also, we are going to add the overtime hours to our index.html.erb table header:

<!-- app/views/posts/index.html.erb -->

<thead>
  <tr>
    <th>
      #
    </th>
    <th>
      Overtime Requested
    </th>
    <th>
      Date
    </th>
    <th>
      User
    </th>
    <th>
      Rationale
    </th>
    <th>
      Status
    </th>
    <th></th>
    <th></th>
  </tr>
</thead>

Make this change in _post.html.erb partial as well.

<!-- app/views/posts/_post.html.erb -->

<tr>
  <td>
    <%= post.id %>
  </td>
  <td>
    <%= post.overtime_request %>
  </td>
  <td>
    <%= post.date %>
  </td>
  <td>
    <%= post.user.full_name %>
  </td>
  <td>
    <%= truncate(post.rationale) %>
  </td>
  <td>
    <%= status_label post.status %>
  </td>
  <td>
    <%= link_to 'Edit', edit_post_path(post), id: "edit_#{post.id}" if policy(post).update? %>
  </td>
  <td>
    <%= link_to 'Delete', post_path(post), method: :delete, id: "delete_post_#{post.id}_from_index", data: { confirm: 'Are you sure?' } %>
  </td>
</tr>

Resources