iie labs

Testing ActiveResource - Basic Tutorial

Posted over 8 years ago by Philip Ingram

Note: This post may be the first post ever to NOT use an @matz example in it, when demonstrating how to use httpmock in your Test Unit tests.

ActiveResource Testing Basics

In order to test ActiveRecord, you don’t want to hit your server side app all the time. It’d just suck.

ActiveResource comes with a mocking Class that helps us get around this problem, by faking (mocking) the RESTful action you are performing.

Read more about HttpMock.

Setting up to Test ActiveResource

One missing little piece of information is that you have to do this around the top of your test_helper.rb file:

require 'active_resource/http_mock' 

Using HttpMock in your Tests

I like, and use, fixtures but it appears we can’t use them, for some reason. You’ll notice your tests are a little ‘cludgier’ then you may like. But it’s the end result we are after.

Here’s some actual code i pulled from a project. All these tests pass. The controller is just a boring old scaffold, nothing fancy.

So, for a typical scaffold/CRUD with RESTful actions controller, here is what i use in my functional tests.


require 'test_helper'

class LinkInfosControllerTest < ActionController::TestCase
def setup
#setup the variables here
@index = [{ :id => 1, :user_id => 1,
:website_id => 1,
:website_base => “http://www.yahoo.com”,
:price => 1,
:currency => ‘USD’ }].to_xml(:root => ‘link_infos’)
@show = { :id => 1, :user_id => 1,
:website_id => 1,
:website_base => “http://www.yahoo.com”,
:price => 1,
:currency => ‘USD’ }.to_xml(:root => ‘link_info’)

#match variables to REST methods. ActiveResource::HttpMock.respond_to do |mock| mock.get “/link_infos.xml”, {}, @index mock.get “/link_infos/1.xml”, {}, @show mock.delete “/link_infos/1.xml”, {}, nil mock.put “/link_infos/1.xml”, {}, nil end end test “should get index” do get :index assert_response :success assert_not_nil assigns(:link_infos) end test “should show link_info” do get :show, :id => 1 assert_response :success end test “should get edit” do get :edit, :id => 1 assert_response :success end test “should update link_info” do link = LinkInfo.find(1) link.price = 14 assert_equal(14, link.price) assert_equal(true, link.save) end test “should destroy link_info” do assert LinkInfo.find(1).destroy assert_response :success end

end

Notice that the @index variable is ‘wrapped’ in an array. Without this “collection” of hashes (I just used one hash, but you get the point), you technically aren’t calling all now are you? No you aren’t, also, the fact that the type=array is sent via your server side app is also used to determine if ActiveResource is using a collection or not.

This is how i understand it, but i’m sure there is a better way to put it, leave a comment and I’ll fix this statement.

If you use curl to talk to your server side app, you’ll see what i mean. Do something like (replace localhost:3001 with whatever your self.site is set to, and replace models with your modelname):


curl http://localhost:3001/models.xml

That’s it for now….

Yeah, I seem to be blogging about ARes a lot now, so i’m sure i’ll have a part 2 as i get more into it.

Comments

blog comments powered by Disqus