<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Moves On Rails: Testing a REST full activeresource</title>
    <link>http://www.movesonrails.com/articles/2007/07/04/testing-a-rest-full-activeresource</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>New ways to look at software</description>
    <item>
      <title>Testing a REST full activeresource</title>
      <description>&lt;p&gt;Active Resources are nice, nut when you're trying to test your active resources , you're in for a long night. Luckally someone did a lot of the basic work for us. The HTTPMock class in ActiveResource is a very handy tool for testing your active resources. Unfortunately there is absolutely no documentation about how to use it, and it has some behaviour you might not expect. Therefore I present you with this code example.&lt;/p&gt;

&lt;p&gt;The remote models of our REST connection live in Connections::IO::REST. When requesting the Employees via Connections::IO::REST::Employee.find(:all), we are actually requesting /employees.xml from our HTTPMock class. The HTTPMock class then just outputs the XML file of the testset we got from our friend which wrote the REST-connector we are connecting to. In the example, this is: /test/remote_fixtures/connector/employees.xml&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; class IORestConnectionTest &amp;lt; Test::Unit::TestCase

 def setup
   @pre      = Connections::IO::REST
   @mock_url = 'http://localhost/'
   @headers  = {"User-Agent"=&amp;gt;"Moves", "Accept-Encoding"=&amp;gt;"deflate", "Content-Type"=&amp;gt;"application/xml"}
   ActiveResource::HttpMock.respond_to do |mock|
     mock.get "/employees.xml", @headers, ioconnect_resources('employees')
     mock.get "/clients.xml"  , @headers, ioconnect_resources('clients')
   end

   # Overwrite all site URLS
   @pre::Resource.site = @mock_url
 end

 def ioconnect_resources(name)
   path = File.join(RAILS_ROOT, "test", "remote_fixtures", "ioconnect", "#{name.to_s}.xml")
   return nil unless File.exists?(path)
   File.read path
 end

 def teardown
     ActiveResource::HttpMock.reset!
 end

 def test_should_find_all_employees_via_rest
   employees = @pre:Employee.find(:all)
   assert_equal employees.count, 20
 end

 end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;One last note: HTTPMock was created to be as simple as possible. If you request  something with a different header or parameters in the URL, it will return absolutely nothing. So if you get no response from your mock activeresource, be sure to check the headers you sent.&lt;/p&gt;</description>
      <pubDate>Wed, 04 Jul 2007 15:38:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:b7187f5b-d67e-45aa-b365-e5ad650a2957</guid>
      <author>bart.tenbrinke@movesonrails.com (Bart ten Brinke)</author>
      <link>http://www.movesonrails.com/articles/2007/07/04/testing-a-rest-full-activeresource</link>
      <category>Rails</category>
      <category>REST</category>
      <category>Resource</category>
      <category>ActiveResource</category>
      <category>Test</category>
      <category>HTTPMock</category>
    </item>
    <item>
      <title>"Testing a REST full activeresource" by dovadi</title>
      <description>Ah, I was struggling with the same problem myself a few weeks ago. I found the tests (examples) with HttpMock in the code of the ActiveResource gem itself and used it within Rspec (although without the use of fixtures). I agree, no documentation, so it was a good idea to post this!!</description>
      <pubDate>Wed, 04 Jul 2007 22:05:21 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:82289087-27ef-440f-861e-a228a22587ee</guid>
      <link>http://www.movesonrails.com/articles/2007/07/04/testing-a-rest-full-activeresource#comment-36</link>
    </item>
  </channel>
</rss>
