如何使用 Cucumber 来测试 Devise 的 Rememberable 功能?
我想要一个 Cucumber 功能来测试 devise(记住我 cookie)的可记住功能。
使用水豚检查“记住我”复选框很容易,但是我应该如何模拟用户在关闭窗口后返回网站?
I'd like to have a Cucumber feature testing the rememberable functionality of devise (a remember me cookie).
It is easy to check the remember me check box using capybara but how should I simulate a user returning to the site after closing their window?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想出了以下机架测试 hack 和稍微干净的 selenium api 使用,来测试 Cucumber/capybara 中的 Devise 记住我功能。它只是告诉驱动程序手动删除会话 cookie。并非所有驱动程序都受支持,我只实现了我使用过的两个:
http://gist.github.com/ 484787
这假定会话的 cookie 存储。从场景中删除 @announce 标签以消除冗长。
另一种选择是 Matt Wynne 在邮件列表讨论中建议的,可能正在查看其他 cookie 存储,并通过查询或文件删除来删除它们:
从敏捷 Rails 书籍中删除:
或者
Rails 也有一个重置会话方法,但我相信我们无权访问此方法,因为我们无法挂钩使用水豚测试时进入 Rails 会话。
希望这有帮助,
尼克
I came up with the following rack-test hack, and slightly cleaner selenium api use, to test Devise remember-me functionality in cucumber/capybara. It just tells the driver to manually erase the session cookie. Not all drivers are supported, I only implemented the two I've used:
http://gist.github.com/484787
This assumes cookie storage of the session. Remove @announce tag from the scenario to get rid of the verbosity.
Another option, suggested by Matt Wynne in the mailing list discussion, may be looking at other cookie stores, and deleting them by query or file deletion:
lifted from agile rails book:
or
Rails also has a reset session method, but I believe we don't have access to this because we can't hook into the rails session when testing with capybara.
Hope this helps,
Nick
nruth 的要点确实很有帮助,但我觉得按名称删除 cookie 是作弊。我创建了一个步骤,删除浏览器关闭并重新启动时将删除的 cookie(任何没有设置到期日期并在将来设置的 cookie)。
您可以在此提交中看到它(尽管我只为 RackTest 驱动程序做过因为我没有 Selenium 设置)。您还可以在此提交中查看我的login/remember_me功能。我将这些类重构为此提交中的单独文件。
我希望这有帮助。
nruth's gist was really helpful but I felt like deleting the cookie by name was cheating. I created a step that deletes the cookies a browser would delete when it was closed and restarted (any cookie without an expiry date set and set in the future).
You can see it in this commit (though I've only done it for the RackTest driver as I don't have Selenium setup). You can also see my login/remember_me feature in this commit. And I refactored the classes to separate files in this commit.
I hope that's helpful.
我使用 email-spec 来完成此任务。我的场景如下所示:
请注意场景上方的 @allow-rescue 装饰,这是使用 Devise 时必需的。
希望这有帮助。
I used email-spec to accomplish this. My Scenario looks like the following:
Note the @allow-rescue decoration above the scenario that is necessary when using Devise.
Hope this helps.
我想您可以使用水豚注销,然后重新登录,例如
This should use cabybara's current cookie state to model this flow。
I guess you could log out with capybara, and then log back in something like
This should use cabybara's current cookie state to model this flow.
您可以使用 show_me_the_cookies 来实现此目的,如下所示:
You can use show_me_the_cookies for this as shown below: