globalkeith
2010-Jan-12 22:04 UTC
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! -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Jan-13 15:45 UTC
Re: tips on how to write a controller test for models associ
Keith Salisbury wrote:> I have a controller test here: > > http://gist.github.com/275616Why? 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. --0015174760d8296c2c047d0da9a2 Content-Type: text/plain; charset=ISO-8859-1 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. --0015174760d8296c2c047d0da9a2--
Kura Monni
2010-Jan-14 16:23 UTC
Re: tips on how to write a controller test for models associ
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. -- Posted via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Jan-15 02:20 UTC
Re: tips on how to write a controller test for models associ
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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Jan-15 02:21 UTC
Re: tips on how to write a controller test for models associ
Marnen Laibow-Koser wrote:> Given /I have \d+ self-esteem/ do |n| > current_user.self_esteem = 0 > endEr, sorry, I meant current_user.self_esteem = n.to_i But you get the idea. :) Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Jan-15 02:22 UTC
Re: tips on how to write a controller test for models associ
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 > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org-- Posted via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Kura Monni
2010-Jan-20 16:49 UTC
Re: tips on how to write a controller test for models associ
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 > endThis 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 :D -- Posted via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Jan-20 17:11 UTC
Re: tips on how to write a controller test for models associ
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 :DBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. --00151747b52a0a9804047d9bae6a Content-Type: text/plain; charset=ISO-8859-1 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. --00151747b52a0a9804047d9bae6a--
Michal Burak
2010-Apr-21 17:26 UTC
Re: tips on how to write a controller test for models associ
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. -- Posted via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.