RSpec :想要对一次性方法进行多次测试,包括夹具

发布于 2024-09-28 18:02:35 字数 547 浏览 8 评论 0原文

我正在开发 Rails 应用程序。 我想用 rspec 测试我的方法“开始!”我的模型备份。

所以这里的规范(也都是方法):

  • Backup 应该将文件复制到文件夹 /backups
  • Backup 应该检查 md5 sum
  • Backup 应该删除原始文件文件

对于我的测试,我根据固定装置创建假文件:

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

然后,@backup.start!应该删除其中一些文件(不是全部)。

问题是:我不想为每个测试重新运行所有操作! 我可以编写一个大型测试,其中包含所有要求,但这会很难看...

before(:all) 模式,上下文中的事件在所有上下文之前运行,而固定装置则不是出于设计原因,可用。

有什么建议吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青衫负雪 2024-10-05 18:02:35

如果每个方法都是独立的,则可以模拟除一个方法之外的所有方法,并每次更改该方法。

您可以选择并限制您想要测试的方法。

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.

罪#恶を代价 2024-10-05 18:02:35

感谢 http://thefrontiergroup。 com.au/blog/2010/01/using-rspec-example-groups-for-common-functionity/

我找到了解决方案:

# /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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文