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 <[email protected]> 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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to