RSpec : want to have many tests for one shot method, including fixtures
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 /backupsBackup
should check md5 sumBackup
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.
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.
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
精彩评论