Testing login using cucumbers

I am trying to test login functionality using Cucumber. My users_steps.rb file contains

Given /^I am a user named "([^"]*)" with an email "([^"]*)" and password "([^"]*)"$/ do |name, email, password| u = User.new(:name => name, :email => email, :password => password, :password_confirmation => password) u.skip_confirmation! u.save! end When /^I sign in as "(.*)\/(.*)"$/ do |email, password| #Given %{I am not logged in} When %{I go to the sign in page} And %{I fill in "user_email" with "#{email}"} And %{I fill in "user_password" with "#{password}"} And %{I press "Log Me In"} end Then /^I should be signed in$/ do Then %{I should see "Sign out"} end Then /^I should be signed in$/ do Then %{I should see "Sign out"} end Then /^I sign out$/ do visit('/account/logout') end 

and my cucumber scenario:

  Scenario: User signs in successfully with email Given I am not logged in And I am a user named "foo" with an email " user@test.com " and password "please" When I go to the sign in page And I sign in as " user@test.com /please" Then I should be signed in When I return next time Then I should be already signed in 

However, this test cannot sign the user. I checked that the user was created correctly, but after filling out the form, I was redirected to the login page.

I am using capybara. What am I missing?

+6
source share
1 answer

You probably want to use:

1. In the user_steps.rb file located in step_definitions:

 Given /^a valid user$/ do @user = User.create!({ :email => " minikermit@hotmail.com ", :password => "12345678", :password_confirmation => "12345678" }) end Given /^a logged in user$/ do Given "a valid user" visit signin_url fill_in "Email", :with => " minikermit@hotmail.com " fill_in "Password", :with => "12345678" click_button "Sign in" end 

In your authentication function:

 Scenario: Login Given a valid user When I go to the login page And I fill in the following: |Email| minikermit@hotmail.com | |Password|12345678| And I press "Sign in" Then I should see "Signed in successfully." 

Remember to change the path to your login page at support / paths.rb

 when /the login page/ user_session_path 

Here my path uses the default setting. You can use rake routes to find out your login.

You may need to change the text in β€œSign In,” β€œSigned Successfully,” to fit your page. My assumptions here are that you are using the default configuration for cucumber + capybara + devise.

+2
source

Source: https://habr.com/ru/post/891282/


All Articles