I need to have different settings for my unit tests and different settings for my integration tests. Example
For unit tests, I would like to do
WebMock.disable_net_connect!(:allow_localhost => true)
And for integration tests, I would like to do
WebMock.allow_net_connect!
Also, before the start of an integration test, I would like to make sure that solr is started. Hence I want to be able to call
config.before(:suite) do
SunspotStarter.star开发者_如何转开发t
end
BUT, only for integration tests. I do not want to start my solr if its a unit test.
How do I keep their configurations separate? Right now, I have solved this by keeping my integration tests in a folder outside the spec folder, which has its own spec_helper. Is there any better way?
My solution might be a bit hackish, but as far as I tested it should work.
I noticed that config.include
takes a type
argument, so it could be abused to execute arbitrary blocks of code like so:
module UnitTestSettings
def self.included(base)
WebMock.disable_net_connect!(:allow_localhost => true)
end
end
module IntegrationTestSettings
def self.included(base)
WebMock.allow_net_connect!
RSpec.configure do |config|
config.before(:suite) do
SunspotStarter.start
end
end
end
end
Rspec.configure do |config|
config.include UnitTestSettings, :type => :model
config.include IntegrationTestSettings, :type => :integration
end
Drop this in a file in the support folder and you should be good to go, although I haven't actually tested the code. Also, I'm quite sure there is a better way to achieve the same.
You can specify a type for a before / after block, in the same way that you can for the include statement. So you could do the following:
RSpec.configure do |config|
config.before(:each, type: :model) do
WebMock.disable_net_connect!(:allow_localhost => true)
end
config.before(:each, type: :request) do
WebMock.allow_net_connect!
end
config.before(:suite, type: :request) do
SunspotStarter.start
end
end
精彩评论