在带有标签的套件钩子之前使用 RSpec
我试图将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该有效:
This should work:
我没有深入研究它,但我的猜测是 :suite 作用域可能在解释任何标签之前运行。据我所知,无论如何你只需要一个 :suite (尽管我可以在你的示例中看到用法)。我认为可以做一个 before :all 会做同样的事情,只要你把它放得足够“高”,就像你的 spec_helper.rb 中所说的那样?
==编辑==
所以我又想了一些,我脑海中浮现出一个解决方案是这样的:
我做了类似的事情来使用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:
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!