黄瓜、rspec、webrat if else 在匹配器上

发布于 2024-11-26 15:53:47 字数 512 浏览 0 评论 0原文

我的黄瓜测试套件中有一个步骤定义,用于对用户进行身份验证并尝试加快我的测试套件的速度,我希望此步骤检查用户是否已通过身份验证。也许这是一个坏主意,因为这意味着测试可能相互依赖......我不知道。但我该怎么做呢?不幸的是,我对 Ruby 不太熟悉。

我希望我可以在 DOM 中渲染一个类,这样我就可以使用 have_selector 或其他东西对此进行测试。这也可能是一个坏主意......

Given /^I am authenticated as a "([^"]*)" user$/ do |role|
  #TODO: only do this if the user is not authenticated
  visit('/login')
  fill_in "name", :with => "something"
  fill_in "pass", :with => "password"
  click_button
  response_body.should contain("Log out")
end

I have a step definition in my cucumber test suite that authenticates the user and to try and speed up my test suite, I want this step to check the user is already authenticated. Perhaps this is a bad idea because it means tests may depend on each other... I don't know. But how would I do this? I am not fluent in Ruby, unfortunately.

I am hoping that I can render a class in the DOM so I can test against this with have_selector or something. This might also be a bad idea too...

Given /^I am authenticated as a "([^"]*)" user$/ do |role|
  #TODO: only do this if the user is not authenticated
  visit('/login')
  fill_in "name", :with => "something"
  fill_in "pass", :with => "password"
  click_button
  response_body.should contain("Log out")
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸵鸟症 2024-12-03 15:53:47

你是对的,这将是一个坏主意。但是,如果您确实想这样做,正确的方法是检查页面上的任何类型的文本,例如您可以在状态栏上显示已登录的用户名。这是正确的方法,因为它模拟了人类的行为,例如 - 哦,这是我的用户名 - 这意味着我已经登录了:)

您的步骤如下所示:

 Given /^I am authenticated as a "([^"]*)" user$/ do |role|
   unless page.has_content?(username)
     visit('/login')
     fill_in "name", :with => "something"
     fill_in "pass", :with => "password"
     click_button
     response_body.should contain("Log out")
   end
 end

You're right this would be a bad idea. But if you really want to do it the correct way would be checking any kind of text on the page, for example you can have status bar with logged in username. This would be correct way because it simulates the behavior of a human, something like - oh, here is my username - it means that I'm already logged in:)

Here is how your step could look like:

 Given /^I am authenticated as a "([^"]*)" user$/ do |role|
   unless page.has_content?(username)
     visit('/login')
     fill_in "name", :with => "something"
     fill_in "pass", :with => "password"
     click_button
     response_body.should contain("Log out")
   end
 end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文