Skip to content
Switch branches/tags
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Ruby DnsMock

Maintainability Test Coverage CircleCI Gem Version Downloads GitHub Contributor Covenant

πŸ’Ž Ruby DNS mock. Mimic any DNS records for your test environment and even more.

Table of Contents


  • Ability to mimic any DNS records (A, AAAA, CNAME, MX, NS, PTR, SOA and TXT)
  • Zero runtime dependencies
  • Lightweight UDP DNS mock server with dynamic/manual port assignment
  • Test framework agnostic (it's PORO, so you can use it outside of RSpec, Test::Unit or MiniTest)
  • Simple and intuitive DSL


Ruby MRI 2.5.0+


Add this line to your application's Gemfile:

group :development, :test do
  gem 'dns_mock', require: false

And then execute:

$ bundle

Or install it yourself as:

$ gem install dns_mock


# Example of mocked DNS records, please follow this data structure
records = {
  '' => {
    a: %w[],
    aaaa: %w[2a00:1450:4001:81e::200e],
    ns: %w[],
    mx: %w[], # you can specify host(s) or host(s) with priority
    txt: %w[txt_record_1 txt_record_2],
    cname: '',
    soa: [
        mname: '',
        rname: '',
        serial: 2_035_971_683,
        refresh: 10_000,
        retry: 2_400,
        expire: 604_800,
        minimum: 3_600
  '' => {
    ptr: %w[]

# Main DnsMock interface
# records:Hash, port:Integer, exception_if_not_found:Boolean
# are optional params. By default creates dns mock server with
# empty records. A free port for server will be randomly assigned
# in the range from 49152 to 65535, if record not found exception
# won't raises. Returns current dns mock server
dns_mock_server = DnsMock.start_server(records: records) # => DnsMock::Server instance

# returns current dns mock server port
dns_mock_server.port # => 49322

# interface to setup mock records.
# Available only in case when server mocked records is empty
dns_mock_server.assign_mocks(records) # => true/nil

# interface to reset current mocked records
dns_mock_server.reset_mocks! # => true

# interface to stop current dns mock server
dns_mock_server.stop! # => true

# returns list of running dns mock servers
DnsMock.running_servers # => [DnsMock::Server instance]

# interface to stop all running dns mock servers
DnsMock.stop_running_servers! # => true


Require this either in your Gemfile or in RSpec's support scripts. So either:

# Gemfile
group :test do
  gem 'rspec'
  gem 'dns_mock', require: 'dns_mock/test_framework/rspec'


# spec/support/config/dns_mock.rb
require 'dns_mock/test_framework/rspec'

DnsMock RSpec helper

Just add DnsMock::TestFramework::RSpec::Helper if you wanna use shortcut dns_mock_server for DnsMock server instance into your RSpec.describe blocks:

# spec/support/config/dns_mock.rb
RSpec.configure do |config|
  config.include DnsMock::TestFramework::RSpec::Helper
# your awesome first_a_record_spec.rb
RSpec.describe FirstARecord do
  subject(:service) do
      dns_gateway_host: 'localhost',
      dns_gateway_port: dns_mock_server.port

  let(:hostname) { '' }
  let(:first_a_record) { '' }
  let(:records) { { hostname => { a: [first_a_record] } } }

  before { dns_mock_server.assign_mocks(records) }

  it { eq(first_a_record) }

DnsMock RSpec interface

If you won't use DnsMock::TestFramework::RSpec::Helper you can use DnsMock::TestFramework::RSpec::Interface directly instead:

DnsMock::TestFramework::RSpec::Interface.start_server  # creates and runs DnsMock server instance
DnsMock::TestFramework::RSpec::Interface.stop_server!  # stops current DnsMock server instance
DnsMock::TestFramework::RSpec::Interface.reset_mocks!  # resets mocks in current DnsMock server instance
DnsMock::TestFramework::RSpec::Interface.clear_server! # stops and clears current DnsMock server instance


Bug reports and pull requests are welcome on GitHub at This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct. Please check the open tikets. Be shure to follow Contributor Code of Conduct below and our Contributing Guidelines.


The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the DnsMock project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.



DnsMock uses Semantic Versioning 2.0.0