Search code examples
ruby-on-railsrspecfactory-botguard

Triggering a monolithic factory spec on a model change using Guard


I wanted to find a way to test my factories and came across this great article on thoughtbot to accomplish this:

 # spec/factories_spec.rb

FactoryBot.factories.map(&:name).each do |factory_name|
  describe "The #{factory_name} factory" do
     it 'is valid' do
      build(factory_name).should be_valid
     end
  end
end

I'm not too privy when it comes to altering my Guardfile and was just wondering what would be the best way to get this to work. I've noticed something set by default for controllers but nothing for models:

watch(rails.controllers) do |m|
  [
    rspec.spec.call("routing/#{m[1]}_routing"),
    rspec.spec.call("controllers/#{m[1]}_controller"),
    rspec.spec.call("acceptance/#{m[1]}")
  ]
end

Solution

  • Yup, this should work

    watch(%r{app/models/.*\.rb}) do
      [`spec/factories_spec.rb`]
    end