[rspec-users] rspec test for converting a hash to a URL string

Jamie D jam5t3r.lists at gmail.com
Mon Apr 14 21:58:58 EDT 2008


Hi all, I've written an expectation for a method that converts a hash
into a url string of name/value pairs. The problem is that the hash is
not traversed in the same order as it is defined so I can not work out
how to test for the correct returned string. The operation of the code
does not require and specific order so I am not going to add a
specific order to my method.


# test
def mappings_data
  {
    :shell =>         true,
    :ftp =>           10,
    :sql =>           11,
    :email =>         12,
    :subdomains =>    13,
    :parkeddomains => 14,
    :addondomains =>  15,
    :transfer =>      16
  }
end

it "should map arguments to a url" do
  @whm.map_args_to_url(mappings_data).should
eql("?shell=true&ftp=10&sql=11&email=12&subdomains=13&parkeddomains=14&addondomains=15&transfer=16")
end

# implementation
def map_args_to_url(args={})
  '?' + args.map { |k,v| "%s=%s" % [URI.encode(k.to_s),
URI.encode(v.to_s)] }.join('&') unless args.blank?
end

# string that is returned
"?parkeddomains=14&shell=true&email=12&addondomains=15&ftp=10&subdomains=13&transfer=16&sql=11"


More information about the rspec-users mailing list