【问题标题】:How to write "should not" see a page in cucumber?如何写“不应该”看到黄瓜中的页面?
【发布时间】:2013-09-23 17:58:19
【问题描述】:
例如,当用户已经登录时,用户不应该看到登录页面。我应该如何在 cucumber 中实现此功能?
Scenario: Authorizzation
Given I am logged in
Then I should not be asked to authenticate
Given(/^I am logged in$/) do
@user = User.new
end
Then(/^I should not be asked to authenticate$/) do
# what to write???
end
假设认证页面被称为登录。
【问题讨论】:
标签:
ruby-on-rails
ruby
rspec
cucumber
bdd
【解决方案1】:
Then /^(?:|I )should not be on (.+)$/ do |page_name|
URI.parse(current_url).path.should_not == path_to(page_name)
end
【解决方案2】:
在登录页面上,似乎应该出现“登录”或“登录”等按钮。
因此,登录后,您可以检查此按钮是否不存在。
page.should_not have_xpath(:xpath, '//here your xpath to element', :text => "Login")
【解决方案3】:
我认为您从根本上是从错误的方向接近它。相反,您可以断言 I should see 是您期望的东西,例如链接、文本或其他。你也可以follow 一个更高级别的链接,你可以断言你也有适当的访问权限。
很难测试没有发生的事情并且你没有证据,所以测试你所期望的,而不是你无法测试的事物的世界。
【解决方案4】:
没有看到您现有的代码,很难假设。但它是沿着这些路线
Given...do
...
arg1.should #=> should
...
end
Given...do
...
arg1.should_not #=> should not
...
end
更新
Then(/^I should not be asked to authenticate$/) do
# what to write???
unless @user.logged_in? #=> assumes there is a method that checks it
routines to login
end
end
根据我的实践经验,我建议您掌握 Ruby 基础知识来享受黄瓜。此外,当场景说明应该发生什么时,它非常有用。
Scenario: Authentication
Given user is not logged in
Then he should be asked to authenticate #=> (OR) Then he should be redirected to login page
【解决方案5】:
类似:
page.body.should_not include_text("Please enter your credentials")