Skip to content

Commit

Permalink
Reorganize RSpec helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Feb 17, 2011
1 parent 6124e84 commit 91e7919
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 59 deletions.
62 changes: 3 additions & 59 deletions spec/spec_helper.rb
Expand Up @@ -5,66 +5,10 @@
SPEC_ROOT = File.expand_path("../", __FILE__)
end

module Helpers

private

# Temporary resets Server @@definitions
# to let the test setup a custom definition list.
def with_definitions
definitions_setup
yield
ensure
definitions_teardown
end

def definitions_setup
@_definitions = Whois::Server.definitions
Whois::Server.send :class_variable_set, :@@definitions, { :tld => [], :ipv4 =>[], :ipv6 => [] }
end

def definitions_teardown
Whois::Server.send :class_variable_set, :@@definitions, @_definitions
end


# Temporary resets parser @@registry
# to let the test setup a custom registry.
def with_registry
@_property_registry = Whois::Answer::Parser::Base.send :class_variable_get, :@@property_registry
Whois::Answer::Parser::Base.send :class_variable_set, :@@property_registry, {}
yield
ensure
Whois::Answer::Parser::Base.send :class_variable_set, :@@property_registry, @_property_registry
end

# Gets the currently described class.
# Conversely to +subject+, it returns the class
# instead of an instance.
def klass
described_class
end

def fixture(*names)
File.join(SPEC_ROOT, "fixtures", *names)
end

end

module ConnectivityHelpers
def need_connectivity
if connectivity_available?
yield
end
end

def connectivity_available?
ENV["ONLINE"].to_i == 1
end
end
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[File.join(SPEC_ROOT, "support/**/*.rb")].each { |f| require f }

RSpec.configure do |config|
config.mock_with :mocha
config.include Helpers
config.extend ConnectivityHelpers
end
15 changes: 15 additions & 0 deletions spec/support/helpers/connectivity_helper.rb
@@ -0,0 +1,15 @@
module ConnectivityHelper
def need_connectivity
if connectivity_available?
yield
end
end

def connectivity_available?
ENV["ONLINE"].to_i == 1
end
end

RSpec.configure do |config|
config.extend ConnectivityHelper
end
49 changes: 49 additions & 0 deletions spec/support/helpers/spec_helper.rb
@@ -0,0 +1,49 @@
module SpecHelper

private

# Temporary resets Server @@definitions
# to let the test setup a custom definition list.
def with_definitions
definitions_setup
yield
ensure
definitions_teardown
end

def definitions_setup
@_definitions = Whois::Server.definitions
Whois::Server.send :class_variable_set, :@@definitions, { :tld => [], :ipv4 =>[], :ipv6 => [] }
end

def definitions_teardown
Whois::Server.send :class_variable_set, :@@definitions, @_definitions
end


# Temporary resets parser @@registry
# to let the test setup a custom registry.
def with_registry
@_property_registry = Whois::Answer::Parser::Base.send :class_variable_get, :@@property_registry
Whois::Answer::Parser::Base.send :class_variable_set, :@@property_registry, {}
yield
ensure
Whois::Answer::Parser::Base.send :class_variable_set, :@@property_registry, @_property_registry
end

# Gets the currently described class.
# Conversely to +subject+, it returns the class
# instead of an instance.
def klass
described_class
end

def fixture(*names)
File.join(SPEC_ROOT, "fixtures", *names)
end

end

RSpec.configure do |config|
config.include SpecHelper
end

0 comments on commit 91e7919

Please sign in to comment.