Ruby 之前对rspec进行微调

Ruby 之前对rspec进行微调,ruby,rspec,Ruby,Rspec,在Rspec中,我们可以设置一些全局前/后行为,如下所示: config.before(:each) {blah...} before_controller_and_model = lambda do do_stuff end config.before(:each, :type => :model) { before_controller_and_model.call } config.before(:each, :type => :controller) { b

在Rspec中,我们可以设置一些全局前/后行为,如下所示:

config.before(:each) {blah...}
before_controller_and_model = lambda do
  do_stuff
end

config.before(:each, :type => :model)      { before_controller_and_model.call }
config.before(:each, :type => :controller) { before_controller_and_model.call }
我可以说:

config.before(:each, :type => :model) {blah...}
使此块仅与模型测试一起运行 但这是行不通的:

config.before(:each, :type => [:model, :controller]) {blah...}

我必须重复同样的事情两次,一次是模型,一次是控制器。还有别的办法吗?感谢查看RSpec过滤器:

它们只匹配任意元数据,rspec-rails将元数据:type=>:model(或其他)添加到每组测试中

所以,这个配置

config.before(:each, :type => [:model, :controller]) {blah...}
将仅将测试与此元数据匹配(匹配相同的值):

这基本上意味着你的问题的答案是否定的

不管怎样,你可以很容易地用这样的东西来DIY它:

config.before(:each) {blah...}
before_controller_and_model = lambda do
  do_stuff
end

config.before(:each, :type => :model)      { before_controller_and_model.call }
config.before(:each, :type => :controller) { before_controller_and_model.call }

查看RSpec过滤器:

它们只匹配任意元数据,rspec-rails将元数据:type=>:model(或其他)添加到每组测试中

所以,这个配置

config.before(:each, :type => [:model, :controller]) {blah...}
将仅将测试与此元数据匹配(匹配相同的值):

这基本上意味着你的问题的答案是否定的

不管怎样,你可以很容易地用这样的东西来DIY它:

config.before(:each) {blah...}
before_controller_and_model = lambda do
  do_stuff
end

config.before(:each, :type => :model)      { before_controller_and_model.call }
config.before(:each, :type => :controller) { before_controller_and_model.call }

这个答案应该被接受。来吧,如果你想麻烦别人帮你,你应该坚持到底,给正确的答案打分。这个答案应该被接受。来吧,如果你想麻烦别人帮你,你应该坚持到底,给正确答案打分。