fine tune rspec before
In Rspec, we can setup some global before/after behavior like this:
config.before(:each) {blah...}
I can say:
config.before(:each, :type => :model) {blah...}
To have this block run only with model tests But this wouldn't work:
config.before(:each, :type => [:model, :controller]) {blah...}
I have to repeat the same thing twice, one for model and one for controller. Is there any other way to do开发者_JAVA技巧 this? Thanks
Looking at RSpec filters: https://www.relishapp.com/rspec/rspec-core/docs/hooks/filters
They just match any arbitrary metadata, and rspec-rails adds the metadata :type => :model (or whatever) to each set of tests.
So, this config
config.before(:each, :type => [:model, :controller]) {blah...}
would only match tests with this metadata (matching the same value):
describe "matching tests", :type => [:model, :controller] {blah...}
Which basically means the answer to your question is NO.
Anyway, you could easily DIY it with something like this:
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 }
精彩评论