Models with validates_uniqueness / confirmation constraints

5 views
Skip to first unread message

Tim

unread,
Oct 28, 2009, 10:49:59 PM10/28/09
to Machinist Users
System: Ruby 1.8.7, Rspec 1.1.12, Rails 2.1

Hey, I have an Account which has the following blueprint:
Sham.admin_email { Faker::Internet.email }
Sham.billing_email { Faker::Internet.email }

Account.blueprint do
admin_email= Sham.admin_email
billing_email= Sham.billing_email

name { 'Admin Company' }
admin_prefix { 'Mr' }
admin_first_name { 'Papa' }
admin_last_name { 'Doc'}
admin_email { admin_email }
admin_email_confirmation { admin_email }
admin_password { 'gametight' }
billing_prefix { 'Mrs.' }
billing_first_name { 'Suga' }
billing_last_name { 'Mama' }
billing_email { billing_email }
billing_email_confirmation { billing_email }
billing_address_1 { 'PO BOX 775'}
billing_city { 'Vancouver' }
billing_state_province { 'British Columbia' }
billing_postal_code { 'V7Y 1L2' }
billing_country {'British Columbia'}
billing_phone_number { '444-334-3434' }
country_code { 'CAN' }
phone_number { '444-334-3434' }
end

I also have a user blueprint that defines the account as an
association. In most controller specs, I create a user in my setup
method via: @user= User.make.

However, that yields AR validation errors for every spec complaining
that the Account's admin_email / billing_email not being unique?? Am
I missing something?

Awesome gem/idea by the way. Thanks, Tim

Pete Yandell

unread,
Oct 28, 2009, 11:36:41 PM10/28/09
to Machinist Users
On Oct 29, 1:49 pm, Tim <tim.linqu...@gmail.com> wrote:
> System: Ruby 1.8.7, Rspec 1.1.12, Rails 2.1
>
> Hey, I have an Account which has the following blueprint:
> Sham.admin_email { Faker::Internet.email }
> Sham.billing_email { Faker::Internet.email }
>
> Account.blueprint do
>   admin_email= Sham.admin_email
>   billing_email= Sham.billing_email

That ain't right! I'm surprised that doesn't throw errors.

You want:

Account.blueprint do
admin_email { Sham.admin_email }
billing_email { Sham.billing_email }

You can even shorten that to:

Account.blueprint do
admin_email
billing_email

Tim

unread,
Oct 29, 2009, 4:18:18 PM10/29/09
to Machinist Users
Thanks for the feedback. Actually, that syntax works fine. The
problem (silly me) was that when I was refactoring my test suite I
accidentally moved these two lines so the RAILS_ENV was being set
after the environment file was loaded:

require File.expand_path(File.dirname(__FILE__) + "/../config/
environment")
ENV["RAILS_ENV"] = "test"

I swapped them back and am good to go now.

Thanks, Tim
Reply all
Reply to author
Forward
0 new messages