# Model class conditional on ENV\["RAILS\_ENV"\] == "test"

**URL:** <https://www.ruby-forum.com/t/model-class-conditional-on-env-rails-env-test/94879>\
**Category:** Rails\
**Created:** [May 17, 2006, 12:51am UTC](https://www.ruby-forum.com/t/model-class-conditional-on-env-rails-env-test/94879 "2006-05-17T00:51:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [May 17, 2006, 12:51am UTC](https://www.ruby-forum.com/t/model-class-conditional-on-env-rails-env-test/94879/1 "2006-05-17T00:51:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [May 17, 2006, 2:55am UTC](https://www.ruby-forum.com/t/model-class-conditional-on-env-rails-env-test/94879/2 "2006-05-17T02:55:56Z")

</div>

Take a look at mock objects.

They may be your good friend here.

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [May 17, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/model-class-conditional-on-env-rails-env-test/94879/3 "2006-05-17T16:17:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [May 18, 2006, 12:42am UTC](https://www.ruby-forum.com/t/model-class-conditional-on-env-rails-env-test/94879/4 "2006-05-18T00:42:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [May 17, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/model-class-conditional-on-env-rails-env-test/94879/5 "2006-05-17T16:16:37Z")

</div>

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
