Writing a System Test

Learn how to write a system test for a Rails application.

We'll cover the following...

System testing major flows

We want to test major flows, and there are two that we can see: correctly saving a widget and seeing validation errors. Our system test can’t reasonably test all the back-end business logic, and doesn’t need to exhaustively test each possible error case. We really only need to make sure that all fields that could have an error will show one. Fortunately, we can create a blank widget, and this will show validation errors for all three fields.

Because we don’t have JavaScript, our system test can use the standard test case, ApplicationSystemTestCase.

Press + to interact
# test/system/create_widget_test.rb
require "application_system_test_case"
class CreateWidgetTest < ApplicationSystemTestCase
test "we can create a widget" do
end
test "we can see validation errors" do
end
end

Let’s start with the validation errors because the back end is already faked out to provide errors no matter what. This test will go to the new widget page, skip filling in any fields, click “Create,” and then validate that there are errors for each field.

Press + to interact
# test/system/create_widget_test.rb
end
test "we can see validation errors" do
→ visit new_widget_path
→ click_on("Create")
→ assert_text "The data you provided is not valid"
→ assert_text "Name can't be blank"
→ assert_text "Price is not a number"
→ assert_text "Manufacturer can't be blank"
end
end

We need something to happen when we click “Create,” so let’s ...