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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> I checked the db and it has got 0 entries in > the countries table.Which db? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
What do you mean? I checked the test database and it''s running on a MySQL server with mysql2 gem. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sep 7, 2:55 pm, Heinz Strunk <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 7 Wrz, 12:55, Heinz Strunk <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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. > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote in post #1020639:> On Sep 7, 2:55pm, Heinz Strunk <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 ?) > > FredHey 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.