Model class conditional on ENV["RAILS_ENV"] == "test"

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

Take a look at mock objects.

They may be your good friend here.


– Tom M.

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 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

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