gpt4 book ai didi

ruby-on-rails - 如何为 Rails 编写 cucumber (最佳实践)。特征和步骤

转载 作者:数据小太阳 更新时间:2023-10-29 06:54:43 24 4
gpt4 key购买 nike

我目前正在尝试学习 cucumber 以及如何正确利用它。在搜索最佳实践时,大多数旧方法都被描述了,但我还没有真正找到一个好的指南。我阅读了有关执行此操作的新方法,但我对最佳实践有一些疑问。

以下是我一直在研究的一些基本 cucumber 场景。

Scenario: Unsuccessful login
Given a user has an account
When the user tries to log in with invalid information
Then the user should see an log in error message

Scenario: Successful login
Given a user has an account
When the user logs in
Then the user should see an log in success message
And the user should see a sign out link

Scenario: Successful logout
Given a signed in user
Then the user logs out
And the user should see an log out success message

请问这样可以吗?我有问题,如果我应该把它写成“我访问”或“用户访问”或“他访问”基本上是首选?

其次,我想知道我应该如何制定以下内容:

Scenario: Visit profile of user
Given a user
And a second user
When the user visit the user profile
Then the user should see the name of the user

Scenario: Visit profile of another user
Given a user
And a second user
When the user visit the second users profile
Then the user should see the name of the second user

这只是我放在一起的东西,但我觉得这不是最好的方法。我在步骤定义文件中遇到问题。您将如何定义处理场景的步骤?我想写一些更笼统的东西,但也许真的不可能?我应该有一个@second_user 属性还是你有什么建议?

def user
@user ||= FactoryGirl.create :user
end

Given /^a signed in user$/ do
user
sign_in(@user.email, @user.password)
end

Given /^a user has an account$/ do
user
end


When /^the user logs in$/ do
sign_in(@user.email, @user.password)
end

When /^the user logs out$/ do
click_link ('Sign out')
end

When /^the user tries to log in with invalid information$/ do
sign_in("incorrect-email", "incorrect-password")
end

Then /^the user should see a sign out link$/ do
page.should have_link('Sign out')
end

Then /^the user should see an log in success message$/ do
should have_success_message('Signed in successfully.')
end

When /^the user should see an log out success message$/ do
should have_success_message('Signed out successfully.')
end

Then /^the user should see an log in error message$/ do
should have_error_message('Invalid email or password.')
end

谢谢你帮助我!

最佳答案

I have problems if I should write it as "I visit" or "the user visit" or "he visit" Basically what is preferred?

我认为使用诸如“当用户访问时”之类的内容会更通用并且更具可读性,因为您不必一直思考“谁是‘他’?”如果您正在阅读测试,真正重要的是在您的所有文件中遵守一些约定,这样您就不会感到困惑。

关于是否创建@second_user 的第二个问题,我认为你不应该因为这个用户不完全是你场景的一部分,因为它是数据设置,我认为处理数据设置的最佳方式就是用pickle with cucumber ,它基本上允许您在 cucumber 中创建模型,而不必将它们作为变量保存在您身边,有一个 great cast on RailsCast这解释了很多。

所以我会用 pickle 来做这个

Scenario: Visit profile of another user
Given a user exists with name: "Mike", username: "mike"
And a signed in user
When the user visits the profile of "mike"
Then the user should see 'Mike'

然后你可以定义

When /^the user visits the profile of (.+)$/ do |username|
visit("/#{username}") # I am assuming here usernames are unique and the profile url is "/:username"
end

关于ruby-on-rails - 如何为 Rails 编写 cucumber (最佳实践)。特征和步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13484399/

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