Skip to content

Testing all Factories (with RSpec)

Gasparyan Arsen edited this page Sep 13, 2013 · 27 revisions

To make sure that your models are valid you can automatically test all of your factories with the following code:

require 'spec_helper'

FactoryGirl.factories.map(&:name).each do |factory_name|
  describe "factory #{factory_name}" do
    it 'is valid' do
      factory = build(factory_name)

      if factory.respond_to?(:valid?)
        expect(factory).to be_valid, factory.errors.full_messages.join(',')
      end
    end
  end
end

Clone this wiki locally