gpt4 book ai didi

ruby - capybara 与 cucumber |获取 cookie

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

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

步骤定义

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

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

Controller

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

但它不起作用:

结果

expected: not nil
got: nil

在 RSpec 示例中,一切正常:

Controller 规范

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 值?

谢谢。

Debian GNU/Linux 6.0.4;

ruby 1.9.3;

Ruby on Rails 3.2.1;

cucumber 1.1.4;

cucumber 导轨 1.2.1;

capybara 1.1.2;

机架测试 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

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

已更新

要获取 session 数据,应该执行以下操作:

步骤定义

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

这也很冗长。

关于ruby - capybara 与 cucumber |获取 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9224001/

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