jmera
jmera

Reputation: 684

Using guard-rspec with factory-girl-rails

I'm using factory_girl_rails as a replacement for fixtures in my Rails project. I'm also using guard-rspec to automatically run my specs. Guard is not picking up the changes I make to my factory files (e.g. spec/factories/users.rb)—I have to stop guard, then reinitialize it for it to pick up those changes.

What are some options for avoiding this manual process?

Upvotes: 3

Views: 774

Answers (5)

mnishiguchi
mnishiguchi

Reputation: 2241

I use Dir.glob to get a list of all the file names that I want to monitor. This works for nested folder structure as well.

require 'active_support/inflector'

watch(%r{^spec/factories/(.+)\.rb$}) { |m|
  [
    *Dir.glob("spec/models/#{m[1].singularize}_spec.rb"),
    *Dir.glob("spec/requests/**/#{m[1]}_spec.rb"),
    *Dir.glob("spec/controllers/**/#{m[1]}_controller_spec.rb"),
  ]
}

Upvotes: 0

johnnyhappy365
johnnyhappy365

Reputation: 31

I use this on my macbook, you should add this line in your Guardfile

watch(%r{^spec/factories/(.+)\.rb$}) { rspec.spec_dir }

and it will automatically run the whole specs file.

If you set the block's return value is 'spec/models', it only run the specs under spec/models dir.

Upvotes: 0

Alter Lagos
Alter Lagos

Reputation: 12550

Limiting even more to the specific specs related to the factory, it should be something like:

require 'active_support/inflector'

  watch(%r{^spec/factories/(.+)\.rb$}) do |m|
    %W{
      spec/models/#{m[1].singularize}_spec.rb
      spec/controllers/#{m[1]}_controller_spec.rb
    }
  end

Upvotes: 1

Daniël W. Crompton
Daniël W. Crompton

Reputation: 3518

To add to your own answer:

I limit what it runs, so it doesn't run too much:

watch(%r{^spec/factories/(.+)\.rb$}) { "spec/models" }

or including the relevant controller:

watch(%r{^spec/factories/(.+)\.rb$}) { |m|
  ["spec/models/", "spec/controllers/#{m[1]}_controller_spec.rb"]
}

Upvotes: 5

jmera
jmera

Reputation: 684

After some searching, I stumbled upon this gist and extracted:

watch(%r{^spec/factories/(.+)\.rb$})

Which, according to the documentation, tells the current guard to watch for changes in *.rb files in spec/factories/

Upvotes: 1

Related Questions