Martin Stanley
2006-May-16 22:51 UTC
[Rails] 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 -- Posted via http://www.ruby-forum.com/.
Tom Mornini
2006-May-17 00:54 UTC
[Rails] Model class conditional on ENV["RAILS_ENV"] == "test"
Take a look at mock objects. They may be your good friend here. -- -- Tom Mornini On May 16, 2006, at 3:51 PM, Martin Stanley wrote:> 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Martin Stanley
2006-May-17 14:16 UTC
[Rails] Re: Model class conditional on ENV["RAILS_ENV"] == "test"
Tom Mornini wrote:> Take a look at mock objects. > > They may be your good friend here. > > -- > -- Tom MorniniThanks 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 -- Posted via http://www.ruby-forum.com/.
Martin Stanley
2006-May-17 14:17 UTC
[Rails] Re: Model class conditional on ENV["RAILS_ENV"] == "test"
Tom Mornini wrote:> Take a look at mock objects. > > They may be your good friend here. > > -- > -- Tom MorniniThanks 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 -- Posted via http://www.ruby-forum.com/.
Martin Stanley
2006-May-17 22:42 UTC
[Rails] Re: Model class conditional on ENV["RAILS_ENV"] == "test"
Martin Stanley wrote:> Tom Mornini wrote: >> Take a look at mock objects. >> >> They may be your good friend here. >> >> -- >> -- Tom Mornini > > 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, > MartinWell, 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 -- Posted via http://www.ruby-forum.com/.