每次更改工厂时,我需要在 Guardfile(在带有 RSpec 和 FactoryGirl 的 Rails 应用程序中)中放入什么来检查所有工厂?
我知道可以运行所有型号规范,根据这个问题:Using guard-rspec with factory-girl-rails ,但我只想对它们进行全部检查。
我尝试在 Guardfile 中执行此操作,但这还不够:
watch(%r{^spec/factories/(.+)\.rb$}) {
FactoryGirl.lint
}
提前致谢。
最佳答案
很清楚你想做什么,但那样做是行不通的。
Guard 不在应用程序“内部”运行。它将 RSpec 作为进程运行。调用 FactoryGirl.lint
是在 Guard 内调用,而不是在 RSpec 进程内调用(这正是您想要的)。
这里的“技巧”是创建一个“假”规范文件,除了 linting 之外不执行任何操作,因此您可以执行以下操作:
guard :rspec do
watch(%r{^spec/factories/(.+)\.rb$}) { 'spec/linting_spec.rb' }
end
其工作原理如下:
- 每当工厂文件发生更改时,Guard 都会使用“spec/linting_spec.rb”(或任何您命名的名称)启动 Guard::RSpec。
- Guard::RSpec 只是调用
rspec spec/linting_spec.rb
。
因此,只要rspec spec/linting_spec.rb
按照您想要的方式工作,您就可以像上面一样将其“插入”到防护中。
spec/linting_spec.rb
模型只能有一个“假”RSpec 测试 - 足以触发 linting。
这样您就只需进行 linting - 而无需运行任何模型测试。
关于ruby-on-rails - 如何立即与 guard 和工厂女孩一起检查工厂?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36890780/