水豚&黄瓜 |获取 cookie [英] Capybara & Cucumber | Getting cookies

查看:16
本文介绍了水豚&黄瓜 |获取 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Cucumber 步骤中获取 cookie 值:

I'm trying to get cookie values in the Cucumber step:

步骤定义

When /^I log in$/ do
  # code to log in
end

Then /^cookies should be set$/ do
  cookies[:author].should_not be_nil
end

控制器

class SessionsController < ApplicationController
  def create
    cookies[:author] = 'me'
    redirect_to authors_path
  end
end

但它不起作用:

结果

expected: not nil
     got: nil

在 RSpec 示例中,一切正常:

In the RSpec examples, all works just fine:

控制器规范

require 'spec_helper'

describe SessionsController do
  describe 'create' do
    it 'sets cookies' do
      post :create
      cookies[:author].should_not be_nil
    end
  end
end

如何使用 Capybara 在 Cucumber 步骤中获取 cookie 值?

How can I get cookie values in Cucumber steps using Capybara?

谢谢.

Debian GNU/Linux 6.0.4;

Debian GNU/Linux 6.0.4;

红宝石 1.9.3;

Ruby on Rails 3.2.1;

Ruby on Rails 3.2.1;

黄瓜 1.1.4;

Cucumber-Rails 1.2.1;

Cucumber-Rails 1.2.1;

水豚1.1.2;

机架测试 0.6.1.

Rack-Test 0.6.1.

推荐答案

步骤定义

Then /^cookies should be set^/ do
  Capybara
    .current_session # Capybara::Session
    .driver          # Capybara::RackTest::Driver
    .request         # Rack::Request
    .cookies         # { "author" => "me" }
    .[]('author').should_not be_nil
end

这行得通,但是,我仍在寻找一种不那么冗长的方法.此外,我想知道如何在步骤定义中获取会话数据.

This works, however, I'm still looking for a less verbose way. Moreover, I'd like to know how to get the session data in a step definition.

更新

要获取会话数据,应执行以下操作:

To get the session data one should do the following:

步骤定义

Then /^session data should be set$/ do
  cookies = Capybara
    .current_session
    .driver
    .request
    .cookies

  session_key = Rails
    .application
    .config
    .session_options
    .fetch(:key)

  session_data = Marshal.load(Base64.decode64(cookies.fetch(session_key)))

  session_data['author'].should_not be_nil
end

这也很冗长.

这篇关于水豚&amp;黄瓜 |获取 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆