When I run rspec I get following failure:
Failures:
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.
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 ?)
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).
On Sep 7, 2:55pm, Heinz S. [email protected] wrote:
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.
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!
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.