Skip to content

Instantly share code, notes, and snippets.

@alxndr
Last active Nov 24, 2015
Embed
What would you like to do?
test FactoryGirl factories (including uniqueness)
require 'spec_helper'
non_idempotent_factories = [:the_president] # singletons
broken_factories = [] # just in case
FactoryGirl.factories.map(&:name).reject{|factory_sym| broken_factories.include? factory_sym}.each do |factory_sym|
factory_name = factory_sym.to_s.camelize
describe "The #{factory_name} factory" do
let(:first) { FactoryGirl.build factory_sym }
it 'should be valid' do
expect(first).to be_valid?, first.errors.full_messages.to_sentence
end
describe "creating a second #{factory_name}" do
before do
first.save
end
it 'should be valid' do
second = FactoryGirl.build(factory_sym)
expect(second).to be_valid?, second.errors.full_messages.to_sentence
end
end unless non_idempotent_factories.include? factory_sym
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment