views:

16

answers:

2

Hello,

I'm working on a Rails app. I want to test with rspec my method "start!" of my model Backup.

So here the specs (all are methods, too):

  • Backup should copy files to folder /backups
  • Backup should check md5 sum
  • Backup should delete original files

For my tests, I create fake files, based on fixtures :

MyFile.all.each{|r| system("touch #{r.path}") }

Then, @backup.start! should delete some of this files (not all).

The problem is : I don't want to re-run all the opérations for each test ! I could write one big test, with all the requirements include in it, but it would be ugly...

The before(:all) pattern, event in contexts runs before all contexts, and fixtures are not available, for design reasons.

Any suggestions ?

Thanks.

A: 

If each method are separate, you can mock all except one and change the one each time.

You can choose and limit with method you want test.

shingara
Do you suggest to run tests once at a time ?
Erwan
yes one type by test. One to test the copy, one to test the check etc...
shingara
It sounds like re-running all operations (but one) each time ! ;-)
Erwan
A: 

Thanks to http://thefrontiergroup.com.au/blog/2010/01/using-rspec-example-groups-for-common-functionality/,

I found the solution :

# /spec/support/backuped_files_examples.rb
shared_examples_for "backed up up files" do
  before(:all) do
    # Fixtures are unreachable for now, 
    # but test db can be use and is filled with fixtures
    MyFile.all.each{|r| system("touch #{r.path}") }
    @backup = Backup.create(...) 
    @backup.start!
  end
end

# /spec/models/backup_spec.rb
describe Backup do  
  context " (when #start! have been called)" do

    before(:each) do
      @files_to_backup = MyFile.all(:conditions => (...))
    end

    it_should_behave_like "backed up up files"
    it "should have deleted wanted files" do
      @files_to_backup.each do |file|
        File.exists(file.path).should be_false
      end
    end
  end
end
Erwan