session[] 不会在黄瓜步骤中持续存在

发布于 2024-08-04 21:12:47 字数 314 浏览 3 评论 0原文

每次

Given /blah.../
    ...
    cart = session[:cart] ||= Cart.new
    ...
end

在场景中运行此步骤时,它都会创建一个新的购物车,而不是(从第二次调用开始)从会话中挑选一个。有人知道如何解决这个问题吗?

我在rails 2.2.2,cucumber 0.3.98,webrat 0.4.4

上 在电路中添加硒可能会有所帮助。但是,由于让黄瓜/硒起作用的初步尝试没有成功,我认为在对抗“永远不会起作用的东西”的黑暗力量之前,需要一点智慧的呼唤

Every time this step

Given /blah.../
    ...
    cart = session[:cart] ||= Cart.new
    ...
end

runs in a scenario, it creates a new cart as opposed to (starting from the second call) picking one up from the session. Anyone knows how to fix that?

I am on rails 2.2.2, cucumber 0.3.98, webrat 0.4.4

P.S.
Possibly adding selenium into the circuit might help. But, since a preliminary attempt to get cucumber/selenuim working didn't succeed, I thought a little call for wisdom was in order before standing up against Dark Force of 'things that never just work'

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

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

发布评论

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

评论(2

遇见了你 2024-08-11 21:12:48

在 Webrat 模拟浏览器来测试应用程序的方式中,您不会获得任何会话支持。这意味着在会话哈希中存储信息适用于当前请求,但不会为后续请求存储该信息。

依赖于先前场景中创建的会话并不是真正的测试好方法。您创建的每个场景都应该是独立的。在“给定”步骤中,您应该确保满足这些步骤的先决条件,如果这包括设置购物车,您应该这样做。

另一个提示:如果您想在做出一些断言后进一步测试,您可以在一个场景中连接多个给定/何时/然后块:

Scenario: Foobar
Given the cart exists
When I click checkout
Then I should see '$100'
When I click 'Pay'
Then I should see 'Paid'

将所有场景连接到一个场景中似乎很诱人,但这使得一旦断言之后调试应用程序变得更加困难场景失败。我更喜欢很多简短的场景!

In the way Webrat simulates a browser for testing your application you don't get any session support. This means that storing information in the session hash works for the current request, but the information doesn't get stored for subsequent requests.

Depending on a session which is created in previous scenario's isn't really a good way of testing. Each scenario you create should be self-containing. In the 'Given' steps you should make sure the prerequisites for the steps are met, if this includes setting up a shopping cart, you should do so.

Another tip: you can concatenate several Given/When/Then blocks in one scenario if you want to test further once you made some assertions:

Scenario: Foobar
Given the cart exists
When I click checkout
Then I should see '$100'
When I click 'Pay'
Then I should see 'Paid'

It seems tempting to concatenate all you scenario's in one, but this makes it harder to debug your application once the scenario fails. I prefer a lot of short scenarios!

孤独难免 2024-08-11 21:12:48

没有真正干净的方法来做到这一点,因为黄瓜正在模拟您的浏览器。您的浏览器所知道的所有会话都是一个不透明的 cookie,它会礼貌地转换为每个请求的哈希值。

最好的解决方案是让您的应用程序以通常的方式创建购物车,即登录、选择产品并将其添加到购物车。这一切都可以一步完成,但我会分几步完成,因为它是可重用的,而且因为它更紧密地遵循用户可能所做的事情。

Scenario: Foobar
  Given I am a logged in user
  And I add a "wizbang" to my cart
  When I click checkout
  Then I should see '$100'
  When I click 'Pay'
  Then I should see 'Paid'

如果问题在于维护步骤之间的信息,您可以使用在整个场景中持续存在的 IVar。 IE:

@cart ||= Cart.new

There is not really a clean way to do this because cucumber is simulating your browser. All your browsers knows of sessions is an opaque cookie that rails politely turns into a hash for each request.

The best solution is to make your application create a cart in the usual fashion, that is logging in, selection a product and adding it to the cart. This can all be done in one step, but I would do it in separate steps because it is reusable and also because it follows more closely what your user is likely to have done.

Scenario: Foobar
  Given I am a logged in user
  And I add a "wizbang" to my cart
  When I click checkout
  Then I should see '$100'
  When I click 'Pay'
  Then I should see 'Paid'

If the problem is maintaining information between steps, you can use IVars that will persist through the whole senario. ie:

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