Forum: Ruby on Rails Model class conditional on ENV["RAILS_ENV"] == "test"

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.
Martin S. (Guest)
on 2006-05-17 02:51
I am trying to setup a model class that I want to inherit from
ActveRecord when in the test mode, and not inherit when in production
and/or development mode. The reason for this is so that I can use
fixtures to test that the logic in the model is working correctly, but I
don't want the database table around during production (and normal
development).

I would like to do this the preferred "rails" way.

I've tried a few solutions using mixins and also using a conditional
expression to control the class definition, but anything I come up with
ether doesn't work or looks some shade of ugly. (The worst being to
duplicatee the class and wrap each definition in conditional code).

Any ideas?

Thanks in advance,
Martin
Tom M. (Guest)
on 2006-05-17 04:55
(Received via mailing list)
Take a look at mock objects.

They may be your good friend here.

--
-- Tom M.
Martin S. (Guest)
on 2006-05-17 18:16
Tom M. wrote:
> Take a look at mock objects.
>
> They may be your good friend here.
>
> --
> -- Tom M.

Thanks for your response, Tom. I'm not sure how mock objects will help
me.

I had a look at mock objects in AWDR; the example they show helps if one
needs to redefine specific methods in a class. My problem is that I want
my test class to inherit from AR and my production and development class
to not inherit from AR. I could replace the prod/dev class with a mock
class that replaces the entire class (including the inheritance) - if
Ruby supports this - but this would not be DRY since both classes would
be identical except for the fact that one inherits from AR.

Can you (or anyone) shed any light on this?

Thanks,
Martin
Martin S. (Guest)
on 2006-05-17 18:17
Tom M. wrote:
> Take a look at mock objects.
>
> They may be your good friend here.
>
> --
> -- Tom M.

Thanks for your response, Tom. I'm not sure how mock objects will help
me.

I had a look at mock objects in AWDR; the example they show helps if one
needs to redefine specific methods in a class. My problem is that I want
my test class to inherit from AR and my production and development class
to not inherit from AR. I could replace the prod/dev class with a mock
class that replaces the entire class (including the inheritance) - if
Ruby supports this - but this would not be DRY since both classes would
be identical except for the fact that one inherits from AR. Perhaps I am
missing something?

Can you (or anyone) shed any light on this?

Thanks,
Martin
Martin S. (Guest)
on 2006-05-18 02:42
Martin Stanley wrote:
> Tom M. wrote:
>> Take a look at mock objects.
>>
>> They may be your good friend here.
>>
>> --
>> -- Tom M.
>
> Thanks for your response, Tom. I'm not sure how mock objects will help
> me.
>
> I had a look at mock objects in AWDR; the example they show helps if one
> needs to redefine specific methods in a class. My problem is that I want
> my test class to inherit from AR and my production and development class
> to not inherit from AR. I could replace the prod/dev class with a mock
> class that replaces the entire class (including the inheritance) - if
> Ruby supports this - but this would not be DRY since both classes would
> be identical except for the fact that one inherits from AR. Perhaps I am
> missing something?
>
> Can you (or anyone) shed any light on this?
>
> Thanks,
> Martin

Well, I think I solved my own problem and I'd share the solution with
the list.

The requirement was that the class should inherit from AR when in test
mode only and at the same time not repeat the code for the class. I did
this through defining the class in question as a Module and then mixin
it in to the appropriate class:

    Module Class_with_code
      ...
    end

    if (ENV["RAILS_ENV"] == "test")
      class Class_to_use <  ActiveRecord::Base
        include Class_with_code
      end
    else
      class Class_to_use
        include Class_with_code
      end
    end

I knew there was a simple (and elegant) solution!

Martin
This topic is locked and can not be replied to.