FactoryGirl doesnt produce unique names?!

Hey,

I got following very simple test case:
require 'spec_helper'

describe Country do
  it "should create a new instance given valid attributes" do
    Factory(:country)
  end
end

Factory looks like:
Factory.sequence :country_name do |n|
  "Country #{n}"
end

Factory.define :country do |c|
  c.name Factory.next(:country_name)
  c.nationality "Foo nationality"
  c.association :currency
end

And the model:
class Country < ActiveRecord::Base
  attr_accessible :currency, :code

  belongs_to :currency

  validates :currency_id, :presence => true
  validates :name, :presence => true, :uniqueness => true
end

When I run rspec I get following failure:
Failures:

  1) Country should create a new instance given valid attributes
     Failure/Error: Factory(:country)
     ActiveRecord::RecordInvalid:
       Validation failed: Name has already been taken
     # ./spec/models/country_spec.rb:5:in `block (2 levels) in <top
(required)>'

I have absolutely no idea what's wrong here since the country's name is
generated with a sequence. I checked the db and it has got 0 entries in
the countries table.

I checked the db and it has got 0 entries in
the countries table.

Which db?

What do you mean? I checked the test database and it's running on a
MySQL server with mysql2 gem.

tests run inside a transaction so (by default at least) you can't see
what's in the test database from outside the test. Probably worth
sticking a breakpoint in there so that you can poke around at the
state of the database and check exactly what's going on (e.g. is that
validation error actually coming from the country model or is it
coming from the currency ?)

Fred

To get unique values you need to define name using the sequence in
brackets eg. c.name { Factory.next(:country_name) }. That's way you'll
get country with unique name each time you call Factory(:country).

Frederick Cheung wrote in post #1020639:

tests run inside a transaction so (by default at least) you can't see
what's in the test database from outside the test. Probably worth
sticking a breakpoint in there so that you can poke around at the
state of the database and check exactly what's going on (e.g. is that
validation error actually coming from the country model or is it
coming from the currency ?)

Fred

Hey Fred,

I tried that already and database was empty the whole time. I put the
debugger right before the Factory and followed every step but nothing
and then still the message appears.

What I found out though is when I turned of transactional fixtures with
config.use_transactional_fixtures = false
I found 2 entries in the currencies table:
Currency 1
Currency 1
which has obviously validate uniqueness turned off.

and in the countries table:
Country 1

and then the error so for some reason it must try to create a second
"Country 1" which fails then but I just don't know why twice.

ayiana wrote in post #1020780:

To get unique values you need to define name using the sequence in
brackets eg. c.name { Factory.next(:country_name) }. That's way you'll
get country with unique name each time you call Factory(:country).

Perfect, thanks a lot!