gpt4 book ai didi

ruby-on-rails - 设计/ cucumber - 添加确认用户存在的步骤

转载 作者:行者123 更新时间:2023-12-04 15:37:25 28 4
gpt4 key购买 nike

我是 cucumber 的新手,我发现以下片段来测试设计登录功能。然而,似乎又少了一步,我没有找到任何解决方案:

Given /^that a confirmed user exists$/ do
pending # express the regexp above with the code you wish you had
end

这里的代码如下:

功能/身份验证/session.feature
Feature: Session handling
In order to use the site
As a registered user
I need to be able to login and logout

Background:
Given that a confirmed user exists

Scenario Outline: Logging in
Given I am on the login page
When I fill in "user_email" with "<email>"
And I fill in "user_password" with "<password>"
And I press "Sign in"
Then I should <action>
Examples:
| email | password | action |
| minimal@example.com | test1234 | see "Signed in successfully" |
| bad@example.com | password | see "Invalid email or password" |

Scenario: Logging out
Given I am logged in
When I go to the sign out link
Then I should see "Signed out successfully"

功能/step_definitions/authentication_steps.rb
# Session
Given /^I am logged in$/ do
visit path_to('the login page')
fill_in('user_email', :with => @user.email)
fill_in('user_password', :with => @user.password)
click_button('Sign in')
if defined?(Spec::Rails::Matchers)
page.should have_content('Signed in successfully')
else
assert page.has_content?('Signed in successfully')
end
end

规范/工厂/user.rb
Factory.define :minimal_user, :class => User do |u|
u.username 'minimal'
u.email 'minimal@example.com'
u.password 'test1234'
u.password_confirmation 'test1234'
end

这里是 orginal code 的链接

非常感谢您的帮助!!

最佳答案

您的标题说“验证用户是否存在”,但这可能不是您需要在那里执行的操作 - 您的 Given步骤不需要断言某些事情有效,它们调用代码来为您的场景创建应用程序状态。当然,它们仍然是测试,因为它们仍然可能失败。

我想你正在寻找这样的东西:

Given /^that a confirmed user exists$/ do
Factory.create(:minimal_user)
end

这将从您的工厂定义中创建并保存一个新的确认用户,以便场景的其余部分可以继续。

关于ruby-on-rails - 设计/ cucumber - 添加确认用户存在的步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4083020/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com