forked from collinsauve/mocha-issue-493-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f9848d
commit f7f2c08
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env ruby | ||
require 'minitest/autorun' | ||
require 'mocha' | ||
require 'mocha/minitest' | ||
require_relative 'example' | ||
|
||
class ExampleTest < Minitest::Test | ||
|
||
DOMAIN = 'example.com' | ||
TXT_RECORDS = ['hello', 'world'] | ||
|
||
def test_get_txt | ||
records = TXT_RECORDS.map do |r| | ||
record = mock | ||
record.expects(:data).with.returns(r) | ||
record | ||
end | ||
|
||
dns = mock | ||
dns.expects(:getresources).with(DOMAIN, Resolv::DNS::Resource::IN::TXT).returns(records) | ||
|
||
Resolv::DNS | ||
.expects(:open) | ||
.yields(dns) | ||
.returns(records) # I should not have to do this, and it allows bad implementations to not actually return the results properly | ||
|
||
result = Example.get_txt(DOMAIN) | ||
|
||
assert_equal TXT_RECORDS, result | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env ruby | ||
require 'resolv' | ||
|
||
class Example | ||
def self.get_txt(domain) | ||
resources = | ||
Resolv::DNS.open do |dns| | ||
dns.getresources(domain, Resolv::DNS::Resource::IN::TXT) | ||
end | ||
|
||
resources.map { |r| r.data } | ||
end | ||
end | ||
|
||
if __FILE__ == $PROGRAM_NAME | ||
puts Example.get_txt('google.com') | ||
end |