Is it possible to add “somewhere” a `before(:each)` hook so that all spec file can run it?

前端 未结 2 1587
慢半拍i
慢半拍i 2021-01-01 23:43

I am using Ruby on Rails 3.2.2 and rspec-rails-2.8.1. In order to make my spec files DRY (Don\'t Repeat Yourself) and to seed the test database I would

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-02 00:39

    In the spec_helper.rb:

    RSpec.configure do |config|
    
      #your other config
    
      config.before(:each) do
        #your code here
      end
    end
    

    There is much configuration available. For instance: config.before(:each, :type => [:routing, :controller, :request])

    You can even create your own tags and associate code to it:

    config.around :each, unobstrusive: true do |example|
      Capybara.current_driver = :rack_test 
      example.run
      Capybara.current_driver = :selenium
    end
    

提交回复
热议问题