在带有标签的套件钩子之前使用 RSpec

发布于 2025-01-03 00:32:36 字数 489 浏览 4 评论 0原文

我试图将 Rspec before(:suite) 钩子与这样的标签一起使用:

  config.before(:suite, :selenium => true) do
    #magical awesomeness
  end

但 Rspec 似乎不尊重该标签并运行我只想运行的代码: selenium =>无论如何,都是如此。

奇怪的是,我也在用 :each 钩子做一件非常相似的事情,而且似乎工作得很好:

  config.around(:each, :selenium => true) do |example|
    Capybara.using_wait_time(10) do
      Capybara.using_driver(:selenium) do
        example.run
      end
    end
  end

有人知道我做错了什么吗?

I'm trying to use the Rspec before(:suite) hook with a tag like so:

  config.before(:suite, :selenium => true) do
    #magical awesomeness
  end

but Rspec doesn't seem to respect the tag and runs the code I only want to run with : selenium => true regardless.

The weird part is that I'm doing a very similar thing with the :each hook as well and that seems to work fine:

  config.around(:each, :selenium => true) do |example|
    Capybara.using_wait_time(10) do
      Capybara.using_driver(:selenium) do
        example.run
      end
    end
  end

Anyone know what I'm doing wrong?

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

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

发布评论

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

评论(2

只为一人 2025-01-10 00:32:36

这应该有效:

if config.inclusion_filter[:selenium]
  config.before(:suite) do
    #magical awesomeness
  end
end

This should work:

if config.inclusion_filter[:selenium]
  config.before(:suite) do
    #magical awesomeness
  end
end
夏末 2025-01-10 00:32:36

我没有深入研究它,但我的猜测是 :suite 作用域可能在解释任何标签之前运行。据我所知,无论如何你只需要一个 :suite (尽管我可以在你的示例中看到用法)。我认为可以做一个 before :all 会做同样的事情,只要你把它放得足够“高”,就像你的 spec_helper.rb 中所说的那样?

==编辑==

所以我又想了一些,我脑海中浮现出一个解决方案是这样的:

# spec_helper.rb
# run with the tag :selenium => true when you set your env var RUN_SELENIUM like so:
# %> RUN_SELENIUM=1 bundle exec rspec spec/
config.filter_run_excluding :selenium => true if ENV['RUN_SELENIUM'].nil?

# now your before suite hook can be something along the lines of
config.before(:suite) do
  if ENV['RUN_SELENIUM'].nil?
    ## regular awesomeness
  else
    ## magical awesomeness
  end
end

我做了类似的事情来使用guard和guard-spec来处理标签。当然,您可以使用不带filter_run_exclusion的环境变量,这与以下内容相同:
%> RUN_SELENIUM=1 捆绑执行 rspec --tag 硒规范/
添加配置行只是有助于保持一致。

希望有帮助!

I've not looked deeply into it, but my guess is that the :suite scope may be run before any tags are interpreted. As far as I can think, you'd only want one :suite anyway (although I can see the use in your example). I think it's possible to do a before :all that would do the same thing, so long as you put it "high" enough, like say in your spec_helper.rb?

==EDIT==

So I thought about it some more, and a solution that poped into my head was something like this:

# spec_helper.rb
# run with the tag :selenium => true when you set your env var RUN_SELENIUM like so:
# %> RUN_SELENIUM=1 bundle exec rspec spec/
config.filter_run_excluding :selenium => true if ENV['RUN_SELENIUM'].nil?

# now your before suite hook can be something along the lines of
config.before(:suite) do
  if ENV['RUN_SELENIUM'].nil?
    ## regular awesomeness
  else
    ## magical awesomeness
  end
end

I did a similar thing to handle tags using guard and guard-spec. You can, of course, use the env var without the filter_run_excluding, and that would be the same as:
%> RUN_SELENIUM=1 bundle exec rspec --tag selenium spec/
Adding the config line just helps keep it consistent.

Hope that helps!

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