rails应用程序中的黄瓜测试似乎没有提交表单来记录用户

时间:2014-11-14 14:42:15

标签: ruby-on-rails-4 cucumber webrat

我正在尝试使用黄瓜和webrat测试我的rails应用程序。我正在尝试以已创建并存储在数据库中的用户身份登录测试,我收到以下错误:

然后我看到登录错误消息#features / step_definitions / user_steps.rb:16       期望以下元素的内容包括“欢迎管理员”:

    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'

我更改了代码以使其登录用户而不是由于无效细节而无法登录用户,因为这不起作用,我不确定是否因为它没有提起是否有flash消息。

这是step_definitions文件(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

这是login.feature文件:

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

这是env.rb文件;

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

这是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 个答案:

答案 0 :(得分:0)

好的,所以你的步骤是:

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

您收到的错误是:RSpec::Expectations::ExpectationNotMetError

我认为这只是你的期望是错误的,但至少有点误导。我们实际上应该看到一个错误(类似“无法登录”),但我们正在寻找"Welcome admin"这似乎更像是欢迎信息。

我们可以通过在期望之前放置puts response.body来查看正在呈现的内容来调试它。

也可能是黄瓜没有等待完成请求。我们可以尝试添加find('#some-element'),如果无法找到元素,它会在失败前等待几秒钟。

编辑:另外,我刚刚注意到您运行的功能不是您所包含的功能(login.feature),因此您尝试查看错误消息而不尝试不正确的登录。