【问题标题】:RSpec tests with Factory Girl creating image使用 Factory Girl 创建图像的 RSpec 测试
【发布时间】:2015-02-15 15:45:10
【问题描述】:

在 rails 4.2.0 应用程序中,我使用 RSpec 和 Factory Girl 进行测试。在应用程序中,我使用 Paperclip 进行图像上传。当我使用空白图像运行测试时,图像被放置在公用文件夹中。

有什么方法可以使用 database_cleaner gem 来解决这个问题?

到目前为止,我有这个:

spec\support\factory_girl.rb

RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
 config.before(:suite) do
  begin
    DatabaseCleaner.start
    FactoryGirl.lint
  ensure
    DatabaseCleaner.clean
  end
 end
end

规范\支持\database_cleaner.rb

RSpec.configure do |config|

 config.before(:suite) do
  DatabaseCleaner.clean_with(:truncation)
 end

 config.before(:each) do
  DatabaseCleaner.strategy = :transaction
 end

 config.before(:each) do
  DatabaseCleaner.start
 end

 config.after(:each) do
  DatabaseCleaner.clean
 end
end

【问题讨论】:

    标签: ruby-on-rails ruby rspec factory-bot database-cleaner


    【解决方案1】:

    DatabaseCleaner 用于清理数据库,而不是文件系统。您可以自己删除文件,例如在after 块中。

    after(:each) { FileUtils.rm @file_path }
    

    【讨论】:

    • 哦,对了,我在阅读一些文档时印象深刻。为小费干杯。虽然如何定义 @file_path ?
    • @Koxi,您可以通过各种方式将文件路径传递给 after 块,例如在 before 块中定义它。或者,您可以在创建文件的同一示例中删除文件,并为两者使用相同的路径变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多