Skip to content
canadaduane edited this page Sep 14, 2010 · 11 revisions

Ruby DocTest allows you to put your tests right in with your Ruby code. It also allows you to put tests in a separate documentation file and have the tests run automatically.

See the Example Usage page for a peek at some code.

Use Your “irb” Sessions as Tests

One of the most compelling features of Ruby DocTest over other test suites is the ability to bring Interactive Ruby (irb) sessions into your code via copy/paste and to use those sessions as your tests. Let’s go through an example of this workflow.

Example Workflow

Let’s say we want to create a Digital Object Identifier library that will give us proper bibliographic references, and vice versa. In fact, let’s say we are creating Rich Apodaca’s DOI library, just like he has it:

<pre> module DOI

  1. Convert a doi into a bibliographic reference.
    def biblio_for doi
    doc = Hpricot(open(“http://www.crossref.org/openurl/?id=doi:#{doi}&noredirect=true&pid=ourl_sample:sample&format=unixref”))
journal = (doc/“abbrev_title”).inner_html year = (doc/“journal_issue/publication_date/year”).inner_html volume = (doc/“journal_issue/journal_volume/volume”).inner_html number = (doc/“journal_issue/issue”).inner_html first_page = (doc/“pages/first_page”).inner_html last_page = (doc/“pages/last_page”).inner_html “#{journal} #{year}, #{volume}(#{number}) #{first_page}-#{last_page}” end
  1. Convert a bibliographic reference into a DOI.
    def doi_for journal, year, volume, issue, page
    doc = Hpricot(open(“http://www.crossref.org/openurl/?title=#{journal.gsub(/ /, ‘%20’)}&volume=#{volume}&issue=#{issue}&spage=#{page}&date=#{year}&pid=ourl_sample:sample&redirect=false&format=unixref”))
(doc/“doi”).inner_html end

end

Clone this wiki locally