Ruby on rails 4 rails应用程序中的cucumber测试似乎并没有提交表单让用户登录
我正在尝试使用cucumber和webrat测试我的rails应用程序。我正试图让测试以已创建并存储在数据库中的用户身份登录,但出现以下错误: 然后我看到一条登录错误消息#features/step_definitions/user_steps.rb:16 预期以下元素的内容包含“欢迎管理员”: 我更改了代码,让它登录一个用户,而不是因为无效的详细信息而无法登录一个用户,因为这不起作用,我不确定这是否是因为它没有接收到flash消息 这是步骤定义文件(user_steps.rb) 以下是login.feature文件:Ruby on rails 4 rails应用程序中的cucumber测试似乎并没有提交表单让用户登录,ruby-on-rails-4,cucumber,webrat,Ruby On Rails 4,Cucumber,Webrat,我正在尝试使用cucumber和webrat测试我的rails应用程序。我正试图让测试以已创建并存储在数据库中的用户身份登录,但出现以下错误: 然后我看到一条登录错误消息#features/step_definitions/user_steps.rb:16 预期以下元素的内容包含“欢迎管理员”: 我更改了代码,让它登录一个用户,而不是因为无效的详细信息而无法登录一个用户,因为这不起作用,我不确定这是否是因为它没有接收到flash消息 这是步骤定义文件(user_steps.rb) 以下是logi
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
以下是文件:
# 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
好的,那么你的步骤是:
Then /^I see a login error message$/ do
visit home_path
response.should contain("Welcome admin")
end
您得到的错误是:RSpec::expections::ExpectationNotMetError
我认为这只是一个例子,你的期望是错误的,但至少有点误导。我们实际上应该看到一个错误(比如“无法登录”),但我们正在寻找更像欢迎信息的“欢迎管理员”
我们可以通过将放置response.body
在期望看到呈现的内容之前来调试它
也可能是黄瓜没有等待请求完成。我们可以尝试添加find(“#some element”)
,如果找不到元素,它将等待几秒钟,然后失败
编辑:另外,我刚刚注意到您正在运行的功能不是您包含的功能(
login.feature
),因此您试图在不尝试错误登录的情况下查看错误消息。谢谢,它现在已排序。
# 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
Then /^I see a login error message$/ do
visit home_path
response.should contain("Welcome admin")
end