设计助手在黄瓜步骤定义中不可用

发布于 2024-12-13 02:17:23 字数 361 浏览 0 评论 0原文

我正在使用 Cucumber 来测试我的 Rails 3.1 应用程序,并使用 devise 进行身份验证。 我需要测试当前用户的电子邮件是否与授权期间使用的电子邮件相同。 问题是设计助手在黄瓜步骤定义中不可用。

有没有任何解决方案可以使设计助手在黄瓜步骤定义中可用?

我尝试过这个:

Cucumber::Rails::World.send('define_method', 'current_user') do
    @current_user ||= (session[:user] && User.find(session[:user])) || :false
end

但这没有帮助。

I'm using cucumber to test my rails 3.1 application and i'm using devise for authentication.
I need to test if current user email is the same with the one used during authorization.
The problem is that devise helpers are not available in cucumber step definition.

Is there any solution to make devise helpers available in cucumber step definitions?

i tried this:

Cucumber::Rails::World.send('define_method', 'current_user') do
    @current_user ||= (session[:user] && User.find(session[:user])) || :false
end

but that didn't help.

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

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

发布评论

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

评论(3

街道布景 2024-12-20 02:17:23

将此添加到需要使用它的步骤定义文件中:

World(Devise::TestHelpers)

虽然这使得“sign_in”和“sign_out”方法可用,但实际上无法从 Cucumber 使用它们,因此这不起作用。 devise 文件顶部的注释显示“不要在集成测试中使用 Devise::TestHelpers”。请参阅:https://github.com/plataformatec/devise/blob/master/lib/ devise/test_helpers.rb

不幸的是,cucumber 步骤无法访问 Web 请求对象和会话参数。看来执行此操作的唯一方法是在每个场景开始时重复执行登录 Web 请求。无聊的。我觉得奇怪的是,黄瓜可以直接操作测试轨道应用程序的数据库,但不能操作网络请求/会话。无论如何,这篇文章展示了一个在模块中重构步骤的好例子,可以轻松地由多个步骤共享:http://drnicwilliams.com/2009/04/15/cucumber-building-a -better-world-object/

如果其他人有关于场景如何设置登录状态而无需执行所有水豚网络调用的答案,我很想听听。

Add this to the step definitions file that needs to use it:

World(Devise::TestHelpers)

While this makes the 'sign_in' and 'sign_out' methods available, they can't actually be used from Cucumber, so this DOES NOT WORK. The comments at the top of devise's file says "Do not use Devise::TestHelpers in integration tests". See: https://github.com/plataformatec/devise/blob/master/lib/devise/test_helpers.rb

Unfortunately, cucumber steps don't have access to the web request object and session parameters. It appears the only way to do this is to repeatedly perform the login web requests at the start of each scenario. Boring. I find it odd that cucumber can directly manipulate the test rails app's database but not the web request/session. In any case, this old post shows a good example of refactoring steps to be in a module that can be shared by many steps easily: http://drnicwilliams.com/2009/04/15/cucumber-building-a-better-world-object/

If anyone else has an answer for how a scenario can set the logged in state without having to do all the capybara web calls, I'd love to hear it.

苏别ゝ 2024-12-20 02:17:23

您可以执行以下操作来在 Cucumber 中存根登录:
https://github.com/plataformatec/devise/wiki /How-To:-Test-with-Capybara

将以下行放入 support/env.rb:

include Warden::Test::Helpers

After do
Warden.test_reset!
end

那么你可以这样做:

user = create :user
login_as :user, range: :user

BUTlogin_as 帮助程序不会在您登录后将您重定向到下一页。它只是让您登录。

因此,您必须以登录用户的身份明确转到正在测试的下一页。

因此,您必须执行另一个步骤来明确执行此操作,例如 visit '/'

You can do this to stub log ins in Cucumber:
https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara

put the lines in support/env.rb:

include Warden::Test::Helpers

After do
Warden.test_reset!
end

then you can do:

user = create :user
login_as :user, scope: :user

BUT, the login_as helper does not redirect you to the next page after logging you in. It just logs you in.

So you must explicitly go to the next page you are testing as a logged in user.

So you must do another step to explicitly do that, e.g. visit '/'

国粹 2024-12-20 02:17:23

可能有帮助的是 https://github.com/ngty/cross-stub - 这允许你可以跨进程存根方法,这有时对于黄瓜测试是必要的

Something that may help is https://github.com/ngty/cross-stub - this allows you to stub methods cross process, which is necessary for cucumber testing sometimes

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