Take the 2-minute tour ×

I'm currently working on a project and we started migrating our tests to isolated test (no rails dependency, and using stubs and mocks). The thing is that until all the current tests are being isolated, we have to run the tests together with the isolated tests, which will start the rails environment.

The problem comes when, in the isolated tests, there is a fake class (class Foo; end;), it will override the original class for the rest of the tests.

Example: In the foo_spec.rb we have this line

class Bar; end;

This would override the Bar class for the next nonisolated tests, and would cause a lot of fails.

There are 2 approaches I could figure in order to get rid of this: - either comment out the fake classes when the tests are run with rails env - put the isolated tests in another folder and run them separated from the rest (this would make more sense)

Can you think of a nicer way to deal with this?

share|improve this question

2 Answers 2

we are using rspec (which should not change anything) and have our rails spec located in spec with their own spec_helper.rb file that is loading the env and all the ugly stuff.

in spec_fast folder we have all the spec that can run without rails, with their own spec-helper that only loads our independent lib folder.

for our ci-server we let both spec folder run in a different task:

if Rails.env.test?
  require 'rspec/core/rake_task'
  require 'ci/reporter/rake/rspec'

  RSpec::Core::RakeTask.new(:all_fast) do |t|
    t.pattern = 'spec_fast/**/*_spec.rb'

  RSpec::Core::RakeTask.new(:all_slow) do |t|
    t.pattern = 'spec/**/*_spec.rb'

  task :all => ["ci:setup:rspec", :all_fast, :all_slow]

it should also be possible to just put them into separate subfolders like spec/rails and spec/fast but i did not try it out because it means to do a lot of path-changing in spec-files.

share|improve this answer
Thanks phoet, I think this might be the nicest way to deal with this for now –  Adrian Florian Mar 23 '12 at 9:32
so none of the answers is acceptable for you? –  phoet Mar 23 '12 at 20:53

I don't know if it's right, but I end up not actually assigning such contextual dummy manually created dummy classes to constants.

Instead of:

class Foo


foo = Class.new do

And you can foo.new or foo.class_method to your heart's content. Could be in @foo too. But you aren't assigning it to the constant Foo like ordinary class definition does, you're creating an 'anonymous' class and assigning it to an ordinary variable, scoped to just within the area you need it.

Note: I'm not saying this is "right" way to do things with rspec, I never feel like I know the right thing to do, the right thing to do might be to somehow not create classes like this at all, or use some weird factorygirl thing which I don't understand or something. But when I need to create 'dummy' type classes just to the scope of a particular test or block, that's what I do.

share|improve this answer
You have a good point there, but what if in the Bar class's implementation there is a line saying 'Foo.getX' ? I will not be able to find that class, so your method would not work in this case. –  Adrian Florian Mar 23 '12 at 9:34

Your Answer


