Search⌘ K
AI Features

Our First Test: Get Homepage

Explore how to write and execute your first feature test in Ruby on Rails by creating a user registration feature file, running tests with Cucumber, and implementing step definitions to cover homepage interactions.

We'll cover the following...

Add our first test

The first step is to create a new file for our new feature.

The file can be generated using the following command:

$ touch features/user_registration.feature

We need to add the following text to the newly-generated file:

D
Feature: User registration
As a first-time visitor
I want to be able to create an account
So that I can access the members area
Scenario: I create an account
Given I am on the homepage
And I click on the registration link
When I fill in and submit the registration form
Then I should see a registration confirmation message
And I should receive a confirmation email

Run our test

As mentioned previously, the test guides everything we do moving forward. So, the logical next step will be to run the test and see what happens.

We’ll use the following command to run the test:

$ bundle exec cucumber features/user_registration.feature

Note: The bundle exec can be omitted when running locally, depending on how your application is set up.

Running the test tells us we have one scenario with no step definitions defined yet. It also gives us a nice template to describe what each of those lines in our feature test means.

The SPA widget below shows our sample application. The features folder, along with all its files, has already been created. Clicking the “Run” button executes the tests by running the cucumber command.

Bud1  Glg1Scomp @� @� @� @G.gitlg1ScompK�.gitmoDDblob�B�A.gitmodDblob�B�A.gitph1ScompappIlocblob;(������applg1Scomp�appmoDDblob�B�AappmodDblob�B�Aappph1Scomp�binIlocblob�(������binlg1Scomp�binmoDDblob�B�AbinmodDblob�B�Abinph1Scomp0configIlocblob(������configlg1ScompK�configmoDDblob�O�AconfigmodDblob�O�Aconfigph1Scomp@	config.ruIlocblob�(������dbIlocblob�(������dblg1ScompvdbmoDDblob�B�AdbmodDblob�B�Adbph1ScompfeaturesIlocblob������featureslg1ScompMfeaturesmoDDblob�O�AfeaturesmodDblob�O�Afeaturesph1Scomp0GemfileIlocbloba(������Gemfile.lockIlocblob�������libIlocblob;�������liblg1Scomp	TlibmoDDblob�B�AlibmodDblob�B�Alibph1ScomplogIlocblob��������loglg1ScompzlogmoDDblob�O�AlogmodDblob�O�Alogph1ScomppublicIlocblob�������publiclg1Scomp)publicmoDDblob�B�ApublicmodDblob�B�Apublicph1Scomp@RakefileIlocblob��������	README.mdIlocblob��������scriptIlocblob�������scriptlg1ScompOscriptmoDDblob�O�AscriptmodDblob�O�Ascriptph1ScompstorageIlocbloba�������storagelg1ScompstoragemoDDblob�B�AstoragemodDblob�B�Astorageph1ScomptmpIlocblob;������tmpbwspblob�bplist00�


]ShowStatusBar[ShowPathbar[ShowToolbar[ShowTabView_ContainerShowSidebar\WindowBounds[ShowSidebar		_{{378, 278}, {1136, 674}}	%1=I`myz{|}~��tmplg1Scomp�V�tmpmoDDblob�O�AtmpmodDblob�O�Atmpph1Scomp;�tmpvSrnlongvendorIlocblob�������vendorlg1ScompvendormoDDblob�B�AvendormodDblob�B�Avendorph1Scomp EDSDB `�(0@� @� @�������loglg1ScompzlogmoDDblob�O�AlogmodDblob�O�Alogph1ScomppublicIlocblob�������publiclg1Scomp)publicmoDDblob�B�ApublicmodDblob�B�Apublicph1Scomp@RakefileIlocblob��������	README.mdIlocblob��������scriptIlocblob�������scriptlg1ScompOscriptmoDDblob�O�AscriptmodDblob�O�Ascriptph1ScompstorageIlocbloba�������storagelg1ScompstoragemoDDblob�B�AstoragemodDblob�B�Astorageph1Scomp
The first feature test

Running the test above generates the following steps for us.

Ruby
Given("I am on the homepage") do
pending # Write code here that turns the phrase above into concrete actions
end
Given("I click on the registration link") do
pending # Write code here that turns the phrase above into concrete actions
end
When("I fill in and submit the registration from") do
pending # Write code here that turns the phrase above into concrete actions
end
Then("I should see a registration confirmation message") do
pending # Write code here that turns the phrase above into concrete actions
end
Then("I should receive a confirmation email") do
pending # Write code here that turns the phrase above into concrete actions
end

We put all of them into a steps file. The file is named user_registration_steps.rb and is placed inside the features/step_definitions folder.

Add the steps to the features/step_definitions/user_registration_steps.rb file in the SPA widget above.

Now, if we run the test again, we see a different message. Run the test again by pressing the “Run” button.

Note: If you made changes to the user_registration_steps.rb file using a terminal based editor (vim, nano), you’ll need to use bundle exec cucumber features/user_registrations.feature to execute the test again.

When we rerun the test, the message says it encountered an unimplemented step when it tried to run the feature test.

Given I am on the homepage
  TODO (Cucumber::Pending)

To resolve this error, we need to implement the unimplemented step.