使用Webrat测试时如何处理cookie?

发布于 2024-07-15 13:16:48 字数 925 浏览 5 评论 0原文

我正在使用 Webrat 为基于 Sinatra 的应用程序编写 Cucumber 测试。 对于某些测试,我需要实现一个像

Given I am logged in as admin
When I am visiting "/"
Then I should see "Settings" 

我定义的步骤这样的场景:

Given /^I am logged in as "(.+)"$/ do |user|
    visit "/login"
    fill_in "login", :with => user
    fill_in "password", :with => "123456"
    click_button "Login"
end

When /^I am viewing "(.+)"$/ do |url|
    visit(url)
end

Then /^I should see "(.+)"$/ do |text|
    response_body.should =~ /#{text}/
end

成功时,将创建一个 cookie

response.set_cookie(cookie_name, coockie_value)

,然后当用户尝试通过帮助程序方法访问管理页面时在视图中进行验证:

def logged_in?
    request.cookies[cookie_name] == cookie_value
end

并且看起来 Webrat 不存储 cookie。 测试不会报告任何错误,但会报告“logged_in?” 在视图中总是错误的,就像 cookie 没有保存一样。

难道我做错了什么? 如果这就是 Webrat 的工作方式,那么最好的解决方法是什么?

I'm writing Cucumber tests for a Sinatra based application using Webrat. For some tests I need to implement a scenario like

Given I am logged in as admin
When I am visiting "/"
Then I should see "Settings" 

I define steps like this:

Given /^I am logged in as "(.+)"$/ do |user|
    visit "/login"
    fill_in "login", :with => user
    fill_in "password", :with => "123456"
    click_button "Login"
end

When /^I am viewing "(.+)"$/ do |url|
    visit(url)
end

Then /^I should see "(.+)"$/ do |text|
    response_body.should =~ /#{text}/
end

On success a cookie is created

response.set_cookie(cookie_name, coockie_value)

and then verified in views when user tries to access admin pages via helper method:

def logged_in?
    request.cookies[cookie_name] == cookie_value
end

And it looks like Webrat doesn't store cookies. Tests don't report any error, but "logged_in?" in views is always false, like the cookie was not saved.

Am I doing something wrong? If this is just how Webrat works, what is the best workaround?

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

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

发布评论

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

评论(3

究竟谁懂我的在乎 2024-07-22 13:16:48

真正的问题是 Sinatra 在测试环境中处理会话的方式。 在 Google 小组中搜索讨论,但真正的解决方案是简单地使用:

use Rack::Session::Cookie

并且

enable :sessions

使用 Selenium 很好,但作为 OP 问题的解决方案,它有点过分了。

The real problem is the way Sinatra is treating sessions in the test environment. Search the Google group for the discussion, but the real solution is to simply use:

use Rack::Session::Cookie

and not

enable :sessions

Using Selenium is nice but it's overkill as a solution for the OP's problem.

活雷疯 2024-07-22 13:16:48

解决方法是使用带有 Selenium 后端的 Webrat。 它在单独的 Firefox 窗口中运行所有测试,因此 cookie 或 javascript 不是问题。 缺点是需要额外的时间和资源来运行 Firefox 并执行所有实际的点击、渲染等操作。

The workaround is use Webrat with Selenium back end. It runs all tests in a separate Firefox window, so cookies or javascript is not a problem. The downside is extra time and resources required to run Firefox and do all the real clicks, rendering etc.

氛圍 2024-07-22 13:16:48

您可以对“给定/^我已登录”步骤 hack logged_in?

Given /^I am logged in as "(.+)"$/ do |user|
    visit "/login"
    fill_in "login", :with => user
    fill_in "password", :with => "123456"
    click_button "Login"

    ApplicationController.class_eval <<-EOE
      def current_user
        @current_user ||= User.find_by_name(#{EOE})
      end
    end
EOE
end

有两个缺点:

  1. 将视图级别和控制器级别问题混合在一起确实是很hackish像这样。
  2. 模拟“注销”会很困难

You could have your "Given /^I am logged in" step hack logged_in?:

Given /^I am logged in as "(.+)"$/ do |user|
    visit "/login"
    fill_in "login", :with => user
    fill_in "password", :with => "123456"
    click_button "Login"

    ApplicationController.class_eval <<-EOE
      def current_user
        @current_user ||= User.find_by_name(#{EOE})
      end
    end
EOE
end

There are two downsides:

  1. It's really hackish to mix view-level and controller-level issues like this.
  2. It'll be difficult to mock up "logout"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文