Simplifying Associations

9 views
Skip to first unread message

Andy Waite

unread,
Jan 17, 2010, 8:29:06 AM1/17/10
to pickle
Hi, I'm trying to find a way shorten

Given I am signed up
And an app exists with user: me
And a component exists with name: "Component name to update", app:
that app

to

Given I am signed up
And a component exists with name: "Component name to update", app:
mine

(component belongs_to an app and app belongs_to a user).

My factories are as follows:

Factory.define :app do |a|
a.name 'App X'
a.url 'http://example.com'
a.association :user
end

Factory.define :mine, :parent => :app do |u|
u.name "Andy's App"
u.user :me
end

Factory.define :me, :parent => :user do |u|
u.name 'Andy'
u.email_address 'an...@example.com'
end

Factory.define :component do |c|
c.sequence(:name) {|n| "Component #{n}" }
c.association :app
end

but when I try run the shorter version is seems that app isn't being
set, since the validation fails with "App can't be blank".

Any ideas what could be going wrong?

Andy

fakingfantastic

unread,
Jan 18, 2010, 11:17:53 PM1/18/10
to pickle
I am trying to figure out sonething that I think is sort of related.
If it is not, or if the question is elementary I apologize, I'm pretty
new to pickle. I want to set up a user to be on the many side of a one
to many relationship (company has many users). Something to the effect
of

Given a company exists with name: "my awesome company"
And a user exists with email: "f...@example.com", password: "insecure",
company: that company
And I log in with....

But it doesn't seem to b making the association, as
current_user.company is nil according to ruby debug, but it is working
fine in my controller spec. What is the correct (or simpler) way to do
this. Thanks


On Jan 17, 8:29 am, Andy Wate <andywa...@gmail.com> wrote:
> Hi, I'm trying to find a way shorten
>
>     Given I am signed up
>     And an app exists with user: me
>     And a component exists with name: "Component name to update", app:
> that app
>
> to
>
>     Given I am signed up
>     And a component exists with name: "Component name to update", app:
> mine
>
> (component belongs_to an app and app belongs_to a user).
>
> My factories are as follows:
>
>   Factory.define :app do |a|
>       a.name 'App X'
>       a.url 'http://example.com'
>       a.association :user
>   end
>
>   Factory.define :mine, :parent => :app do |u|
>       u.name "Andy's App"
>       u.user :me
>   end
>
>   Factory.define :me, :parent => :user do |u|
>       u.name 'Andy'

>       u.email_address 'a...@example.com'

Andy Waite

unread,
Jan 19, 2010, 10:58:55 AM1/19/10
to pickle
Have you defined any factories? If so, can you post the code?

Andy

fakingfantastic

unread,
Jan 19, 2010, 4:26:44 PM1/19/10
to pickle
You got it (apologies, i sent that first post from iPhone, so didn't
have my code) -

Scenario: Creating a client adds them to a user's company
Given a user "joe" has registered with "joe....@example.com",
"test01"
And a company "My Company" exists with name: "My Awesome Company"
And I login with "joe....@example.com", "test01"
And I am on the list of clients
When I follow "Add New Client"
When I fill in "Name" with "My Fav Client"
And I press "Create"
Then a client should exist with name: "My Fav Client"
And a client should be in the company's clients

The error

And I press "Create"
undefined method `clients' for nil:NilClass

The controller

def create
@client = Client.new(params[:client])
current_user.company.clients << @client
if current_user.save
flash[:notice] = "Added: #{@client.name}"
redirect_to clients_url
else
render :action => :new
end
end


The Factories

Factory.define :user do |f|
f.first_name "John"
f.last_name "Smith"
f.password "test123"
f.password_confirmation {|u| u.password}
f.email "f...@example.com"
end

Factory.define :client do |f|
f.name "My Fav Client"
end

Factory.define :company do |f|
f.name "My Company"
end

Factory.define :account do |f|
end


I am like new-born baby new to Rspec, Cucumber, Pickle (get it Ian...
"newborn"? Lame joke...) So any pointers on redo-ing anything would be
greatly appreciated.

Thanks
Frank

Reply all
Reply to author
Forward
0 new messages