Forum: Ruby on Rails Unit testing for ActiveResource

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John S. (Guest)
on 2009-05-04 19:33
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?
John S. (Guest)
on 2009-05-06 12:09
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.
This topic is locked and can not be replied to.