Testing ActiveResource - Basic Tutorial

Posted about 1 year 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

ActiveResource is using a collection or not.
Thank you for this tutorial. Had been trying to Test Active Resource but I am new to it, I did encounter a few puzzling questions and even a couple of headaches
Thank you for this tutorial. Had been trying to Test Active Resource but I am new to it, I did encounter a few puzzling questions and even a couple of headaches
I cannot WAIT to read more of this. I mean, you just know so much about this. So much of it Ive never even thought of. You sure did put a new twist on something that Ive heard so much about. I dont believe Ive actually read anything that does this subject as good justice as you just did.
You’ll notice your tests are a little ‘cludgier’ then you may like. But it’s the end result we are after.
C1fa4d275a7fd03fb8ed741cee2e1139 seo
You’ll notice your tests are a little ‘cludgier’ then you may like. But it’s the end result we are after.
One missing little piece of information is that you have to do this around the top of your test_helper.rb file.
In order to test ActiveRecord, you don’t want to hit your server side app all the time. It’d just suck.
Choosing a profession is always a great note of the decision of someone’s life and should not be denied that the photograph is one of the most passionate professions power and needs a lot of concentration and time image capture and clearly are the most efficient of photography, I mentioned that these notes are really useful for a better photographer.

Regards,

I dont believe Ive actually read anything that does this subject as good justice as you just did.
txocyz
I like, and use, fixtures but it appears we can’t use them, for some reason.
Cats are easy to maintain family and animals, their physiology has been particularly well studied, usually resembles that of other mammalian carnivores, but shows several unusual features, probably attributable to the decline of species that cats living in the desert. Thank you very much.
Regards,
Great tutorial, I’m not sure if I understood everything correctly, but thanks anyway.
The controller is just a boring old scaffold, nothing fancy.
I am new to it, I did encounter a few puzzling questions and even a couple of headaches
I dont believe Ive actually read anything that does this subject as good justice as you just did
ll these tests pass. The controller is just a boring old scaffold, n
Hello, This blog is very interesting and enjoyable to read. I am a big fan of the subjects discussed.
In order to test ActiveRecord, you don’t want to hit your server side app all the time. It’d just suck.
ActiveRecord, you don’t want to hit your server side app all the time.
I will bookmark your blog and have my friends check up here often. I am quite sure they will learn lots of new stuff here than anybody else. Thanks for sharing this information.
This is really great! I am hoping to read more helpful articles from you. Thanks..!!!
Really it’s great job.keep it on… Post! Very informative dude. I saw this site while browsing and think it may be relevant..!!!
when demonstrating how to use httpmock in your Test Unit tests.
Apparently together with the improvement from the technologies, individuals now treat shopping as a really breeze, they could have a very consider in stores, in exactly where they are often served, and so they can purchase within the on the internet shop, like for north encounter lovers, I really endorse them to purchase goods amongst North Confront Jakcets Outlet online, because they could get lower price on the biggest extent.Layer folds over are generally highlighted within the entrance of the brand nature.Man-made coat helps to make the fur industry desire improved, individuals are able to see hair, regardless of whether accurate or even bogus, plus they wish to have these products, which sparked very good regarding hair.north face jackets Within the aesthetic of skinheads that will extremism, Donkey Coat in the common garment field.Because the fall while, also pants took over as central figure of favor.The process temperatures will probably be progress ideal after just about every action.
The controller is just a boring old scaffold, nothing fancy.
What a wonderful piece of information Admiring the time and effort you put into your blog and detailed information you offer! I will bookmark your blog and have my children check up here often.
On the other hand allegedly within Milan currently resolve this kind of dilemma.Okazaki, japan biggest retailer Usa ARROWS recently released the popularity associated with backyard manufacturer via Toronto Canada Goose Shared.Whether or not you go looking for what type of adventures, its suitable apparel. Canada Goose Kensington It is possible to pick the a person with hood (some happens with detachable hoods with fake fur also) or without the need of, water-proof or in any other case."Yesterday, Rubaud about the phone expressing editors stated.It has an absolute sweater like sense and that is short and tiny to just take with them.
Carpet cleaning is a task that should not be neglected. The best time to clean your carpet thoroughly is during the spring cleaning when we freshen up our homes. A clean carpet contributes a lot to the fresh look of a house.
Thank you for this tutorial. Active Resource Test was trying but I’m new to this, I did find some intriguing questions and even a couple of headaches….thanks
for best fake online for gift
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.
I did find some intriguing questions and even a couple of headaches….thanks
qqyykze
All these tests pass. The controller is just a boring old scaffold, nothing fancy.
you will like discount chanel handbag and check coupon code available online
is a muscular open-topped beast of a car. Codenamed the DC100 Sport, it is designed to be
must look at this ugg kids boots , for special offer and check coupon code available
for dvd ripper online online
must look at this Cheap Homecoming Dresses for less for promotion code
you love this? chanel bags for cheap online shopping with low price
best for you discount chanel handbag for promotion code and get big save
I am sure you will love wallet mirror , for special offer to get new coupon
This kind of post is very rare.. its so hard to seek a post like this. very informative and the contents are very Obvious and Concise .I will look more of your post
dissertation topics.
Excellent read, I just passed this onto a colleague who was doing a little research on this topic. And he actually bought me lunch because I found it for him. So I should thank you for the free lunch I got.
analysis essay.
Nice, accurate and to the point.Very significant article for us ,I think the representation of this article is actually superb one. This is my first visit to your site and I want to take this moment to say that I really love this blog. It has been a good resource of information for me in my research. Thank you so much admin.
custom banner design.
I was wondering if you ever thought of changing the layout of your blog? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having one or 2 pictures. Maybe you could space it out better?
This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work
Very useful info. Hope to see more posts soon!
it is really very helpful and effective.people will like your article very much.thanks for adding such a wonderful article.
Hi, I’ve been a lurker around your blog for a few months. I love this article and your entire site! Looking forward to reading more!
I appreciate deeply for the kind of topics you post here. Thanks for sharing us such information that is actually helpful. Have a nice day!
Glad to visit your blog. Thanks for great post that you share to us..
Pretty Interesting post. Couldnt be written any better. Thanks for sharing!
I was looking for some topics that are enough popular and finally founded your blog, it has superb topics with great popularity.
I like this painting very much, through the painting we can express our feelings, express our thoughts. Every painting say its own story and I like to discover that story. Thanks
Thanks for sharing this informative post. I really appreciate that.
Thanks for sharing it. Very nice and informative post.
Hello! I could have sworn I’ve been to this site before but after reading through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be bookmarking and checking back often!
This is a really good post. Must admit that you are amongst the best bloggers I have read. Thanks for posting this informative article.
Good post….thanks for sharing.. very useful for me i will bookmark this for my future needs. Thanks.
Its always good to have tips on good blog posting. As I just started posting comments for blog and faced a lot of rejections. I think your suggestion would be helpful for me. I will let you know if this works for me.
Thanks and keep posting such a informative blogs.
Thanks for that important information, its really helpful.
Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.
Pretty Interesting post. Could not be written any better. Thanks for sharing!
You got a really useful blog. I have been here reading for about an hour. I am a newbie and your success is very much an inspiration for me.
I think the error is due to a virus and disabling the firewall in that case will not be an intelligent thing to do.
Good post….thanks for sharing.. very useful for me i will bookmark this for my future needs. Thanks.
Your work is really looking good,I suggest that you take the help of Software development India to develop a software, helping you with the designing work.
Thanks for sharing this useful information! Hope that you will continue with the kind of stuff you are doing.
mnwipo
NMlPqw comment1
Fine information, thanks to the author. This work is really useful and significant.
This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog.
Thanks for sharing your views on the topic. It makes one think and look the other side of the story.
Turn your love of travel into a business. Or monetize your insider knowledge about your home area. No need to be a travel agent. You know the best spots to visit!
Hi … I just stumbled upon your post.. a gud view point.. Hey ur post left me quenching for more Your post really gives out useful knowledge.. thanks
Thanks for the post and information! I think education is important for us so we must prepare the best education for our generation by sharing such great information with eachother!
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.
for burn wmv to dvd to your friends at my estore
Do you mind if I quote a few of your articles as long as I provide credit and sources back to your website? My blog site is in the exact same niche as yours and my users would definitely benefit from some of the information you provide here. Please let me know if this alright with you. Thanks a lot!
The controller is just a boring old scaffold, nothing fancy.
I have gone through your blog it was really very informative. I definitely share your views to my close friends. Keeps up the good work going.
I think you are really an expert in writing unique blogs. I am crazy about your thoughts.
Kabir Tourist provide Car Hire Service,School Cab Service,Taxi Service,Car Rental service in all over Faridabad
S.N Network & Computers provides complete remote solution and management of IT Services and IT Outsourcing business support for growing businesses and companies in Delhi and NCR
This blog is providing beautiful news about about the Liberian Educational Sector. It is nice to know about it this is useful information. And such blogs makes us familiar with the whole world.
It is nice to read the information provided in your blog and i like this information because it is based on reality and i like this information. And it provides knowledge and useful information to the visitors of this site and i would like to visit this site again.
This page is very informative post about how to test Active Resource . I appreciated what you have done here.
Thanks for your sharing
Today when i wakeup i feel very lazy due to browsing last whole day but now i feel happy because my searching comes to end after reading your article.
‘cludgier’ then you may like. But it’s the end result we are after.
The controller is just a boring old scaffold, nothing fancy.sa
My relatives every time say that I am killing my time here at net, but I know I am getting familiarity daily by reading such fastidious articles.
My relatives every time say that I am killing my time here at net, but I know I am getting familiarity daily by reading such fastidious articles
It is my intention to talk about anything related to your products or services here, but secretly become an accomplice, this counts as full disclosure
A very nice and useful information provided through this blog and like such blogs which always provides such educational information and it is nice to read it.