Rspec:在辅助测试中设置 cookie

发布于 2024-10-27 10:01:09 字数 712 浏览 1 评论 0原文

帮助程序方法

  # Determine if this is user's first time
  def first_time?
    cookies[:first_time].nil?
  end

尝试 Rspec 测试

it "returns true if the cookie is set" do
  cookies[:first_time] = "something"
  helper.first_time?().should be(true)
end

错误:

undefined method `cookies' for nil:NilClass

我读到的有关 Rspec 和 cookies 的所有内容都与控制器有关。有什么方法可以在 Rspec 辅助测试中获取/设置 cookie?

(Rspec/Rspec-rails 2.5、Rails 3.0.4)

谢谢!

更新:

找到了关于如何设置cookie的答案,所以我将其留在这里供其他人参考。

我正在寻找的作品:

helper.request.cookies[:awesome] = "something"

仍然不知道如何获取饼干......

Helper Method

  # Determine if this is user's first time
  def first_time?
    cookies[:first_time].nil?
  end

Attempted Rspec test

it "returns true if the cookie is set" do
  cookies[:first_time] = "something"
  helper.first_time?().should be(true)
end

Error:

undefined method `cookies' for nil:NilClass

Everything I've read about Rspec and cookies has to do with the controller. Any way to get/set cookies in Rspec helper tests?

(Rspec/Rspec-rails 2.5, Rails 3.0.4)

Thanks!!

UPDATE:

Found an answer on how to SET cookies, so I'll leave it here for other's reference.

the piece I was looking for:

helper.request.cookies[:awesome] = "something"

Still don't know how to GET cookies...

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

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

发布评论

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

评论(5

生生漫 2024-11-03 10:01:09

我能找到的最好的解释是在这里: https://www.relishapp .com/rspec/rspec-rails/docs/controller-specs/cookies

引用:

rails-3.0.0 至 3.1.0 的推荐指南

  • 通过中的请求和响应对象访问cookie
    规格。
  • 在操作之前使用 request.cookies 来设置状态。
  • 在操作后使用 response.cookies 来指定结果。
  • 在控制器操作中使用 cookies 对象。
  • 使用字符串键。

例子:

# spec
request.cookies['foo'] = 'bar'
get :some_action
response.cookies['foo'].should eq('modified bar')

# controller
def some_action
  cookies['foo'] = "modified #{cookies['foo']}"
end

The best explanation I've been able to find is here: https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/cookies

Quoted:

Recommended guidelines for rails-3.0.0 to 3.1.0

  • Access cookies through the request and response objects in the
    spec.
  • Use request.cookies before the action to set up state.
  • Use response.cookies after the action to specify outcomes.
  • Use the cookies object in the controller action.
  • Use String keys.

Example:

# spec
request.cookies['foo'] = 'bar'
get :some_action
response.cookies['foo'].should eq('modified bar')

# controller
def some_action
  cookies['foo'] = "modified #{cookies['foo']}"
end
扬花落满肩 2024-11-03 10:01:09

我刚刚偶然发现了你的问题(我正在寻找答案)。我成功地尝试了这个:

helper.request.cookies[:foo] = "bar"

I just stumbled upon your question (I was looking for an answer). I tried this successfully:

helper.request.cookies[:foo] = "bar"
叹沉浮 2024-11-03 10:01:09

在 Rails 6.1.3 上,没有一个答案对我有用,但经过多次测试,以下方法有效:

helper.cookies[:foo] = "bar"

对于签名的 cookie:

helper.cookies.signed[:foo] = "bar"

None of the answers worked for me on Rails 6.1.3, but after much testing, the following worked:

helper.cookies[:foo] = "bar"

And for signed cookies:

helper.cookies.signed[:foo] = "bar"
风情万种。 2024-11-03 10:01:09

您获取 cookie 的方式与设置它们的方式相同。我的一项规格示例:

request.cookies[:email].should be nil

You get the cookies the same way you set them. Example from one of my specs:

request.cookies[:email].should be nil
洛阳烟雨空心柳 2024-11-03 10:01:09

在控制器测试中,这是有效的:

@request.cookies[:cookie_name] = "cookie_value"

在 before 块中。

我在此处找到了这个

In a controller test this works:

@request.cookies[:cookie_name] = "cookie_value"

in a before block.

I found this here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文