cucumber test in rails application does not seem to be submitting a form to log a user in

447 views Asked by At

I am trying to test my rails app using cucumber and webrat. I'm trying to get the test to login as a user who is already created and stored in the database, and I'm getting the following error:

Then I see a login error message # features/step_definitions/user_steps.rb:16 expected the following element's content to include "Welcome admin":

    Csa
        Welcome Guest
        Forgot password?
        or
        Register
      HomeJobs
        English
  Cymraeg
    Welcome to CS-Alumni News
  The is where the home page text will go.
   (RSpec::Expectations::ExpectationNotMetError)
  ./features/step_definitions/user_steps.rb:18:in `/^I see a login error message$/'
  features/users/login.feature:9:in `Then I see a login error message'

I changed the code to get it to login a user instead of failing to log in a user due to invalid details, as that wasn't working, and I wasn't sure if it was because it didn't pick up the flash message or not.

Here is the step_definitions file (user_steps.rb)

require 'webrat'

Given /^I do not have an account$/ do
    @user = FactoryGirl.create(:user)
    @user ||= User.where(:email >= @user[:email]).first
    @user.destroy unless @user.nil?
end

When /^I attempt to login with my details$/ do
    visit new_session_path
    fill_in "login-input", :with => "admin" 
    fill_in "password", :with => "taliesin" 
    clicks_button "Login"
end 

Then /^I see a login error message$/ do 
    visit home_path
    response.should contain("Welcome admin")
end

And /^I should not be logged in$/ do
    #response.should_not contain("Welcome admin")
end

Here is the login.feature file:

Feature: Login
    In order to access my account 
    As a user
    I want to be able to login 

        Scenario: User does not have an account
            Given I do not have an account
            When I attempt to login with my details 
            Then I see a login error message
            And I should not be logged in

        Scenario: User enters incorrect Login
            Given I have an account
            And I am not logged in
            When I enter an incorrect Login
            Then I see a login error message
            And I should not be logged in

        Scenario: User enters incorrect password
            Given I have an account
            And I am not logged in
            When I enter an incorrect password
            Then I see a login error message
            And I should not be logged in

        Scenario: User logs in successfully
            Given I have an account
            And I am not logged in
            When I enter my correct login
            And I enter my correct password
            Then I see a successful login message
            When I return to the application 
            Then I should still be logged in

Here is the env.rb file;

require 'cucumber/rails'
require 'webrat'
require 'cucumber/rails/world'
require 'webrat/core/matchers'

Webrat.configure do |config|
config.mode = :rack
config.open_error_files = false
end

World(Webrat::Methods)
World(Webrat::Matchers)

ActionController::Base.allow_rescue = false


begin
  DatabaseCleaner.strategy = :transaction
  rescue NameError
  raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish              to use it."
  end

 Cucumber::Rails::World.use_transactional_fixtures = false

class ActiveSupport::TestCase
setup do |session|
session.host! "localhost:3001"
end
end

rails/blob/master/features/choose_javascript_database_strategy.feature
Cucumber::Rails::Database.javascript_strategy = :truncation

Here is the gemfile:

# Use jquery as the JavaScript library
gem "jquery-rails", "~> 2.3.0"
gem 'jquery-ui-rails', '4.1.2'

# Turbolinks makes following links in your web application faster. Read more:    https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
  end

group :development, :test do
  gem 'rspec-rails', '~> 3.0.0'
  gem 'cucumber-rails', require: false
  gem "webrat"
  gem 'factory_girl_rails'
  gem 'selenium-client'
  gem 'capybara'
  gem 'database_cleaner'
end
1

There are 1 answers

1
Rich Race On BEST ANSWER

Ok, so your step is this:

Then /^I see a login error message$/ do 
   visit home_path
   response.should contain("Welcome admin")
end

And the error you're getting is: RSpec::Expectations::ExpectationNotMetError

I think it's just a case of you're expectation is wrong, but is at least a bit misleading. We should actually see an error (something like "Cannot login") but we are looking for "Welcome admin" which seems more like a welcome message.

We can debug this by placing puts response.body before the expectation to see what is being rendered.

It may also be that the cucumber isn't waiting for the request to finish. We could try adding find('#some-element') which will wait for a few seconds before failing if it cannot find an element.

Edit: Also, I just noticed the feature that you were running isn't the one you included (login.feature) so you're trying to see an error message without attempting an incorrect login.