# Tips on how to write a controller test for models associated with currently logged in user

**URL:** <https://www.ruby-forum.com/t/tips-on-how-to-write-a-controller-test-for-models-associated-with-currently-logged-in-user/181534>\
**Category:** Rails\
**Created:** [January 13, 2010, 11:55am UTC](https://www.ruby-forum.com/t/tips-on-how-to-write-a-controller-test-for-models-associated-with-currently-logged-in-user/181534 "2010-01-13T11:55:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [January 13, 2010, 11:55am UTC](https://www.ruby-forum.com/t/tips-on-how-to-write-a-controller-test-for-models-associated-with-currently-logged-in-user/181534/1 "2010-01-13T11:55:53Z")

</div>

I have a controller test here:

[gist:275616 · GitHub](http://gist.github.com/275616), which works fine when account is an  
independent model, however I want:

an account to be a property of user, ( and created and associated when  
a user is)  
when the user goes to /account/edit it should on edit the account of  
the logged in user -

I can make rails do this, but I’m utterly baffled how I should alter  
this test to represent this new behaviour

I have a user Factory like this:

Factory.define :user do |user|  
user.email { Factory.next :email }  
user.password { “password” }  
user.password\_confirmation { “password” }  
user.association :account  
end

So maybe my set up should create one of those? Although that doesn’t  
sound right if its an AccountController…

Either way, I’m confused, I am trying to do the right thing by all  
this TDD, but its not making it easy…

- any pointers would be great!

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [January 13, 2010, 4:45pm UTC](https://www.ruby-forum.com/t/tips-on-how-to-write-a-controller-test-for-models-associated-with-currently-logged-in-user/181534/2 "2010-01-13T16:45:31Z")

</div>

Keith S. wrote:

> I have a controller test here:
> 
> [gist:275616 · GitHub](http://gist.github.com/275616)

Why? Cucumber features are generally nicer to work with than controller  
tests.

> which works fine when account is an  
> independent model, however I want:
> 
> an account to be a property of user, ( and created and associated when  
> a user is)  
> when the user goes to /account/edit it should on edit the account of  
> the logged in user -

Which authentication library are you using? They tend to have  
instructions on testing. Generally, you’ll need to stub the  
current\_user method.

> I can make rails do this, but I’m utterly baffled how I should alter  
> this test to represent this new behaviour
> 
> I have a user Factory like this:
> 
> Factory.define :user do |user|  
> user.email { Factory.next :email }  
> user.password { “password” }  
> user.password\_confirmation { “password” }  
> user.association :account  
> end
> 
> So maybe my set up should create one of those? Although that doesn’t  
> sound right if its an AccountController…

The point of factories is to create the object you need, with all  
associations. You need an Account with associated User.

> Either way, I’m confused, I am trying to do the right thing by all  
> this TDD, but its not making it easy…

No, you’re not making it easy. 🙂 The principle is to just create the  
objects and stubs you need for your test. Often you can do this by  
noticing where Rails is throwing errors.

> - any pointers would be great!

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [January 14, 2010, 5:23pm UTC](https://www.ruby-forum.com/t/tips-on-how-to-write-a-controller-test-for-models-associated-with-currently-logged-in-user/181534/3 "2010-01-14T17:23:23Z")

</div>

I have a similar problem as I’m just getting into this BDD business. I  
want to write a Cucumber test that refers to the current user by using  
“I”. For example:

Given I am logged in as “noobie”  
And I have 0 self-esteem

How do I make the connection between I and noobie. It’s easy but ugly to  
figure out a workaround tho.

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [January 15, 2010, 3:20am UTC](https://www.ruby-forum.com/t/tips-on-how-to-write-a-controller-test-for-models-associated-with-currently-logged-in-user/181534/4 "2010-01-15T03:20:47Z")

</div>

Kura Monni wrote:

> I have a similar problem as I’m just getting into this BDD business. I  
> want to write a Cucumber test that refers to the current user by using  
> “I”. For example:
> 
> Given I am logged in as “noobie”  
> And I have 0 self-esteem
> 
> How do I make the connection between I and noobie.

You don’t, really: “I” in a Cucumber step usually doesn’t refer to  
anything specific. What you want is probably to use current\_user or  
whatever equivalent method your authentication library provides:

Given /I am logged in as “([^”]+)/ do |username|  
login\_as User.find\_by\_username(username) # or whatever method your  
authentication library says you should use  
end

Given /I have \d+ self-esteem/ do |n|  
current\_user.self\_esteem = 0  
end

If memory serves, the Cucumber docs deal with almost this exact case.  
You might want to review them.

> It’s easy but ugly to  
> figure out a workaround tho.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [January 15, 2010, 3:21am UTC](https://www.ruby-forum.com/t/tips-on-how-to-write-a-controller-test-for-models-associated-with-currently-logged-in-user/181534/5 "2010-01-15T03:21:31Z")

</div>

Marnen Laibow-Koser wrote:

> Given /I have \d+ self-esteem/ do |n|  
> current\_user.self\_esteem = 0  
> end

Er, sorry, I meant  
current\_user.self\_esteem = n.to\_i

But you get the idea. 🙂

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [January 15, 2010, 3:22am UTC](https://www.ruby-forum.com/t/tips-on-how-to-write-a-controller-test-for-models-associated-with-currently-logged-in-user/181534/6 "2010-01-15T03:22:10Z")

</div>

Marnen Laibow-Koser wrote:

> Marnen Laibow-Koser wrote:
> 
> > Given /I have \d+ self-esteem/ do |n|

Yikes! Can’t type today. That would be

Given /I have (\d+) self-esteem/ do |n|

> > current\_user.self\_esteem = 0  
> > end
> 
> Er, sorry, I meant  
> current\_user.self\_esteem = n.to\_i
> 
> But you get the idea. 🙂
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [January 20, 2010, 5:49pm UTC](https://www.ruby-forum.com/t/tips-on-how-to-write-a-controller-test-for-models-associated-with-currently-logged-in-user/181534/7 "2010-01-20T17:49:28Z")

</div>

Marnen Laibow-Koser wrote:

> Given /I am logged in as “([^”]+)/ do |username|  
> login\_as User.find\_by\_username(username) # or whatever method your  
> authentication library says you should use  
> end
> 
> Given /I have \d+ self-esteem/ do |n|  
> current\_user.self\_esteem = 0  
> end

This is something I’ve tried but it fails because of:  
Given I am logged in as “admin” #  
features/step\_definitions/manage\_logins\_steps.rb:11  
undefined method `current_user' for #<ActionController::Integration::Session:0x7f08cb98> (NoMethodError) ./features/step_definitions/manage_logins_steps.rb:19:in`/^I am  
logged in as “([^”]\*)"$/’  
features/topside\_menu.feature:13:in `Given I am logged in as  
“admin”’

current\_user is defined in the ApplicationController class and it’s set  
as a helper\_method, but apparently it’s still not visible in the Webrat  
steps.

> If memory serves, the Cucumber docs deal with almost this exact case.  
> You might want to review them.

Going to have to look it up 😃

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [January 20, 2010, 6:11pm UTC](https://www.ruby-forum.com/t/tips-on-how-to-write-a-controller-test-for-models-associated-with-currently-logged-in-user/181534/8 "2010-01-20T18:11:37Z")

</div>

Kura Monni wrote:

> Marnen Laibow-Koser wrote:
> 
> > Given /I am logged in as “([^”]+)/ do |username|  
> > login\_as User.find\_by\_username(username) # or whatever method your  
> > authentication library says you should use  
> > end
> > 
> > Given /I have \d+ self-esteem/ do |n|  
> > current\_user.self\_esteem = 0  
> > end
> 
> This is something I’ve tried but it fails because of:  
> Given I am logged in as “admin” #  
> features/step\_definitions/manage\_logins\_steps.rb:11  
> undefined method `current_user' for #<ActionController::Integration::Session:0x7f08cb98> (NoMethodError) ./features/step_definitions/manage_logins_steps.rb:19:in `/^I am  
> logged in as “([^"]\*)”$/’  
> features/topside\_menu.feature:13:in `Given I am logged in as  
> “admin”’
> 
> current\_user is defined in the ApplicationController class and it’s set  
> as a helper\_method, but apparently it’s still not visible in the Webrat  
> steps.

So write a function somewhere in features/support that defines it.

> > If memory serves, the Cucumber docs deal with almost this exact case.  
> > You might want to review them.
> 
> Going to have to look it up 😃

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [April 21, 2010, 7:26pm UTC](https://www.ruby-forum.com/t/tips-on-how-to-write-a-controller-test-for-models-associated-with-currently-logged-in-user/181534/9 "2010-04-21T19:26:36Z")

</div>

Kura Monni wrote:

> current\_user is defined in the ApplicationController class and it’s set  
> as a helper\_method, but apparently it’s still not visible in the Webrat  
> steps.

Check this out  
[http://programmers-blog.com/2010/04/21/cucumber-vs-webrat-vs-current\_user-from-authenticatedsystem](http://programmers-blog.com/2010/04/21/cucumber-vs-webrat-vs-current_user-from-authenticatedsystem)

PS. Sorry for resurrecting the thread. I bounced with this stuff for  
like 2 hours and found nothing. Hope others find the solution useful.
