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

I have a controller test here:

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!

Keith Salisbury wrote:

I have a controller test here:

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. :slight_smile: 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,

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.

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 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. :slight_smile:

Best,

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|

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 :smiley:

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 :smiley:

Best,

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

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