views:

42

answers:

2

I am having trouble showing a mock logged in user in cucumber.

When a user is logged in they can, make a post.

My Errors:

(::) failed steps (::)

undefined local variable or method `user' for #<Cucumber::Rails::World:0x85974df4>    (NameError)
./features/step_definitions/tasklist_steps.rb:25:in `/^that I want to post a link$/'
features/tasklist.feature:10:in `Given that I want to post a link'

Failing Scenarios:
cucumber features/tasklist.feature:9 # Scenario: Submitting a Link

1 scenario (1 failed)
4 steps (1 failed, 1 skipped, 1 undefined, 1 passed)
0m0.432s

My Cucumber:

Given /^I am an authenticated user$/ do
  name = 'example'
  email = '[email protected]'
  password = 'secret'

  Given %{I have one user "#{name}" with email "#{email}" and password "#{password}"}
  And %{I go to the user login page}
  And %{I fill in "username" with "#{name}"}
  And %{I fill in "password" with "#{password}"}
  And %{I press "Login"}
end

Given /^that I want to post a link$/ do
  title = 'My revolutionary post'
  website_link = "http://www.google.com"
  category = 'activism'
  user = authenticated user
  And %{I go to the new post page(#{user})}

How can I make a mock of an authenticated user? so that the test will pass?

+1  A: 

The line

user = authenticated user

is the problem. The right-hand side symbol user is undefined. Is authenticated a method here?

UPDATE: Question clarified

In your Cucumber feature you would have a scenario which starts something like this:

Scenario: Post a link as an authenticated user
  Given I am logged in as the following user:
    | name     | John Doe |
    | email    | [email protected] |
    | password | secret |
  When I go to the posts page
  And I follow "New post"
  And I fill in the following:
    | title    | My revolutionary post |
    | category | Activism |

  etc

Then create a step definition like this:

Given /^I am logged in as the following user:$/ do |fields|
  @user = User.create!(fields.rows_hash)
end

Now @user will be available in your subsequent step definitions to complete the scenario. If you need to provide default model instance values for a user (i.e. additional to the 3 I've shown in the scenario) then you should consider using the Factory Girl gem in which case you could create the user like this:

@user = Factory(:user, fields.rows_hash)
bjg
Right. Yah..I guess my question then is, how do I create a mock of that so that it will pass? I'll update above.
Trip
Ah that's amazing. So Factory feeds off of the fields from Cucumber?
Trip
A: 

Seems like you expect

user = authenticated user

to mean something. At least from what you have provided, it doesn't look like this does anything unless there's supposed to be a method elsewhere called "authenticated" that returns a user object.

Warren
Right. Yah..I guess my question then is, how do I create a mock of that so that it will pass? I'll update above.
Trip