rspec before(:each) 钩子 - 有条件地应用 [英] rspec before(:each) hook - conditionally apply

查看:43
本文介绍了rspec before(:each) 钩子 - 有条件地应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 rails_helper.rb 中有以下内容:

I have following in my rails_helper.rb:

RSpec.configure do |config|
  # ...
  config.before(:each, type: :controller) do
    # SOMETHING
  end
end

我想定义这个 SOMETHING 将适用的目录(在我的情况下仅适用于 spec/controllers/api 目录下的文件).

I want to define directories, to which this SOMETHING will be applicable (in my case ONLY to files under spec/controllers/api directory).

有机会实现吗?

推荐答案

您可以为您的 RSpec 过滤器:

RSpec.configure do |config|
  # ...
  config.before(:each, :subtype => :controllers_api) do
    # SOMETHING
  end
end

然后在 spec/controllers/api 中的 RSpec 示例中,添加一些元数据:

And then in your RSpec examples in spec/controllers/api, you add some metadata:

RSpec.describe "something", :subtype => :controllers_api do
end

SOMETHING 应仅在具有 :subtype => 的示例上运行.:controllers_api 元数据.我没有使用 :type 因为它可能定义了其他行为.

This SOMETHING should run only on examples having the :subtype => :controllers_api metadata. I did not use :type because there may be other behavior defined with it.

这篇关于rspec before(:each) 钩子 - 有条件地应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆