...

/

Making the Capybara Test Pass

Making the Capybara Test Pass

Learn about integration testing with capybara, controller method, how to make design decisions, and how to make the test pass.

Integration testing with Capybara

Let’s go through the integration-test process.

Note: We’re using factory_bot (see Adding Data to Tests) to create the projects and tasks with let!. We could also use fixtures, but we’ve decided the factories are more readable if slower for this go-around.

The first failure is that we don’t have a show method in the ProjectsController. The show method is easy enough and probably doesn’t need additional testing:

Press + to interact
def show
@project = Project.find(params[:id])
end

We also want a template. We know it will need a table for the tasks as well as a form to create a new task. Here’s one. It’s unstyled, but it’s got the table and the form to create a new task:

module ApplicationCable
  class Connection < ActionCable::Connection::Base
  end
end
Adding code to show.html.erb file

At this point, we fail because the create task doesn’t exist on TasksController. That means we need to create new logic.

Controller method

This seems like it’ll be acceptable enough not to need additional tests. At this point, we’ll often do a mini spike of the controller method to see how much complexity is called for. In this case, the controller method is straightforward:

Press + to interact
class TasksController < ApplicationController
def create
@task = Task.new(params[:task].permit(:project_id, :title, :size))
redirect_to(@task.project)
end
end

Note: After adding the code to the SPA widget, you can rerun the code using the following command (don’t forget to save and run the code again, using the ”Save” and “Run” ...