简体   繁体   English

重写 Rails 中的辅助方法

[英]Overriding helper method in Rails

I have following line in my rails helper .我的rails helper有以下行。

  config.use_transactional_fixtures = true

But I want to modify it to但我想将其修改为

  config.use_transactional_fixtures = false

But I don't want to modify it for every tests inside spec folder.但我不想为spec文件夹中的每个测试修改它。 I just want to apply it to all the tests inside spec/requests folder.我只想将它应用于spec/requests文件夹中的所有测试。
How can I do it ?我该怎么做 ?

After doing a bit more research, it looks like you need to use the DatabaseCleaner gem https://github.com/DatabaseCleaner/database_cleaner .经过更多研究后,您似乎需要使用 DatabaseCleaner gem https://github.com/DatabaseCleaner/database_cleaner In your rails helper, you would add this:在您的 rails 助手中,您将添加以下内容:

require 'database_cleaner'

RSpec.configure do |config|
  config.use_transactional_fixtures = false

  config.before type: :request do
    DatabaseCleaner.strategy = :truncation
  end

  config.after type: :request  do
    DatabaseCleaner.strategy = :transaction
  end

  config.before :each do
    DatabaseCleaner.start
  end

  config.after do
    DatabaseCleaner.clean
  end
end

This will set you up so that request specs will use the truncation strategy (removing all data from the database) and everything but request specs use transactions (rollback all changes from runnning scenario).这将设置您,以便请求规范将使用截断策略(从数据库中删除所有数据)并且除请求规范之外的所有内容都使用事务(从运行场景中回滚所有更改)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM