# Unit testing for ActiveResource

**URL:** <https://www.ruby-forum.com/t/unit-testing-for-activeresource/167140>\
**Category:** Rails\
**Created:** [May 4, 2009, 5:33pm UTC](https://www.ruby-forum.com/t/unit-testing-for-activeresource/167140 "2009-05-04T17:33:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [May 4, 2009, 5:33pm UTC](https://www.ruby-forum.com/t/unit-testing-for-activeresource/167140/1 "2009-05-04T17:33:07Z")

</div>

I’m using ActiveResouce objects which don’t have a corresponding  
ActiveRecord object in the DB because they’re acting as a front end to a  
legacy Windows application which can read and write REST type XML.

That works fine, but when I try to do some unit testing  
thenActiveSupport::TestCase when it’s setting up will try to delete  
everything in the tables corresponding to ActiveRecords that it knows  
about. Which it should do in preparation to loading fixtures. But in the  
case of ActiveResources there’s no corresponding table, so it tries to  
delete all records in a table, with the name that corresponds to the  
ActiveResource, and then fails because it’s not there.

E.g. I have an ActiveResouce defined as

class ChoiceCustomers \< ActiveResource::Base  
etc  
end

Now when I try to run the unit test ChoiceCustomerTest \<  
ActiveSupport::TestCase

even though there’s nothing in it apart from the default test, then I  
get an error from the database when it tries to “delete from  
choice\_customers” because there’s no corresponding table.

How can I make sure that ActiveSupport::TestCase doesn’t regard  
ActiveResources as ActiveRecords?

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [May 6, 2009, 10:09am UTC](https://www.ruby-forum.com/t/unit-testing-for-activeresource/167140/2 "2009-05-06T10:09:10Z")

</div>

John S. wrote:

> I’m using ActiveResouce objects which don’t have a corresponding  
> ActiveRecord object in the DB because they’re acting as a front end to a  
> legacy Windows application which can read and write REST type XML.
> 
> That works fine, but when I try to do some unit testing  
> thenActiveSupport::TestCase when it’s setting up will try to delete  
> everything in the tables corresponding to ActiveRecords that it knows  
> about. Which it should do in preparation to loading fixtures. But in the  
> case of ActiveResources there’s no corresponding table, so it tries to  
> delete all records in a table, with the name that corresponds to the  
> ActiveResource, and then fails because it’s not there.
> 
> E.g. I have an ActiveResouce defined as
> 
> class ChoiceCustomers \< ActiveResource::Base  
> etc  
> end
> 
> Now when I try to run the unit test ChoiceCustomerTest \<  
> ActiveSupport::TestCase
> 
> even though there’s nothing in it apart from the default test, then I  
> get an error from the database when it tries to “delete from  
> choice\_customers” because there’s no corresponding table.
> 
> How can I make sure that ActiveSupport::TestCase doesn’t regard  
> ActiveResources as ActiveRecords?

Problem solved!

When you create a new resource via script/generate resource. It doesn’t  
actually create a resource, it creates an ActiveRecord with associated  
controller. So I went in an changed \<ActiveRecord::Base to  
\<ActiveResource::Base in the model file. But…

The generator has also created a dummy fixture.yml in fixtures. So when  
you run tests it tries to delete all records from the corresponding  
table name it infers from the name of the fixture file. In the case of a  
client only ActiveResource application, there isn’t one so it fails.

All I had to do was removed the dummy fixture file and it’s now fine.

John S.
