views:

74

answers:

1

When I run "rake", it's loading up one of the models among all of the classes I have in my app/models directory. The thing is, it's not one I have a test for, it's just a model I have in there that is actually used with script/runner to run in the background and perform tasks for my main Rails application. At the end of the file I've got it creating a new instance of the class above and then running main for the class.

Since it loops indefinitely I definitely do not want it started up by the testing code. Why would the unit testing or Rake involve this other class in any way?

To my shame, I haven't been writing any tests for this code and I decided I would start writing some, but this stopped me right away because I can't even run Rake for what is out there now without it going haywire.

+1  A: 

I'm not sure it's Rake's fault - I have a feeling that when you add :environment as a dependency, you're bringing up the whole Rails infrastructure, which may well involve requiring every model file (this is fairly wild guesswork - I haven't followed the boot process that deeply yet).

However it's happening, it seems that your model is being required, at which point all hell breaks loose.

Looking at script/runner and more usefully, railties/lb/commands/runner.rb, the execution sequence seems to be something like:

require 'boot' # boot the Rails app
eval(File.read(code_or_file)) # run what you asked for

That second line (it's actually around line 45 in runner.rb) looks like the key. How would it be if you defined a separate script (in /lib, say?) that contained the code that runs your model? I think that would probably be a more Rails-ish way to do it. And it would probably stop Rake messing up your tests...

Mike Woodhouse
OK, I have learned from this. Currently I've got script/runner simply loading the class in question and at the end of the class it creates a new instance and starts its loop. So Rake loading the class becomes a bad thing. I'll change that so script/runner starts specifically what I need started.
John Munsch