views:

29

answers:

1

First off - I know that this is generally horrible practice, because Cucumber is meant to only test outputs. I just want to do this for one veryveryvery specific case.

I have an app that handles user authentication using a cookie set by another app I maintain. I'd like to write a very simple integration test for authentication:

Given I have logged as "some_user" on the SSO server
When I visit any page
Then I should be logged in as "some_user"

My current step definition for the Then is as follows:

Then /^I should be logged in as "([^"]*)"$/ do |username|
  user = User.find_by_username(username)
  assert_equal @controller.current_user, user
end

Obviously, this is failing with "Undefined method 'current_user' for nil:NilClass".

In case it's not obvious - ApplicationController#current_user returns either the user currently logged in or nil.

A: 

You can visit some page where currently logged on user's name is displayed.
Like:
When I go to the account page
Then I should see "Welcome user" within "div#login"

Art Shayderov
I'd considered doing this - the app's header displays different things to a logged-in user - but it feels brittle and hacky to me. I'd rather test that someplace else/in addition to testing, directly, whether a user is logged in.
bhaibel
Why do you think it is hacky? If you would test your application in browser, I mean simulating a real user, how would you check current logged on user? I think that's what Cucumber is for, simulating real clicks.
Art Shayderov
bhaibel