Pickle issue with Cucumber Background

28 views
Skip to first unread message

Rick

unread,
Sep 20, 2010, 3:42:42 PM9/20/10
to pickle
Hi,

I'm having issues using my model in a scenario with pickle while using
a background. I've tried multiple ways of defining things, but keep
ending up with the following error:

The model: 'user: "me"' is not known in this scenario. Use
#create_model to create, or #find_model to find, and store a reference
in this scenario. (Pickle::Session::ModelNotKnownError)

Here is a sample gist that I'm using: http://gist.github.com/588520

The error is thrown for the 'Then I should have 1 contact' statement.
I'm using Mongoid and Machinist. From my test logs, I can see that the
user is created by Pickle and being saved to the database.

Am I'm missing something obvious here?

Thanks,
/rick

Ian White

unread,
Sep 20, 2010, 5:12:34 PM9/20/10
to pickle-...@googlegroups.com
Hi there,

I don't think this is a Background issue.

Although I haven;t seen the code for your log in steps, it looks like your first line should be something like

Given a user: "me" exists
# so that the 2nd line 'I' uses this user

Or, change the 2nd line to

And the user is logged in
# so that it refers to the user created in the 1st line

It all depends on how you;ve implemented the log in steps.

Hope that helps.

Cheers,
Ian

> --
> You received this message because you are subscribed to the Google Groups "pickle" group.
> To post to this group, send email to pickle-...@googlegroups.com.
> To unsubscribe from this group, send email to pickle-cucumb...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pickle-cucumber?hl=en.
>

Rick Salsa

unread,
Sep 20, 2010, 7:21:55 PM9/20/10
to pickle-...@googlegroups.com
Hmm, I started over again, and it appears like cucumber isn't picking up the Pickle step definitions. So I see this when I run the test:

You can implement step definitions for undefined steps with these snippets:

Given /^a user exists$/ do
  pending # express the regexp above with the code you wish you had
end

This should match the following correct?

# create a model
Given(/^#{capture_model} exists?(?: with #{capture_fields})?$/) do |name, fields|
  create_model(name, fields)
end

What's weird is if I add what cucumber suggests into features/step_definitions/support/pickle_steps.rb right under the above statement, cucumber sees it. Again, am I missing something here? Running:

rails 3
cucumber (0.8.5)
cucumber-rails (0.3.2)
pickle (0.4.2)

ran rails g pickle --paths --email

Thanks and apologize for the newbieness with Pickle. Thanks!


Rick Salsa

unread,
Sep 22, 2010, 3:35:08 AM9/22/10
to pickle-...@googlegroups.com
Ok,

So I managed to get my things working, but have one further question. So I have my own step now written as:

Given /^#{capture_model} is logged in$/ do |name|
  @user = create_model(name)
  steps %Q{
    Given I am logged in
  }
end

and 

Given /^I am logged in$/ do
  And %{I go to login}
  And %{I fill in "user_email" with "#{@user.email}"}
  And %{I fill in "user_password" with "#{@user.password}"}
  And %{I press "Sign in"}
end

Orginally, I had just added Given a user exists in the first step instead of directly calling create_model. In the logged in step, I couldn't figure out how to reference the newly created users. Is what I'm doing above the best way to accomplish this, or is there a better way?

Thanks!

On Mon, Sep 20, 2010 at 2:12 PM, Ian White <ian.w...@gmail.com> wrote:

Michael MacDonald

unread,
Sep 22, 2010, 7:10:45 AM9/22/10
to pickle-...@googlegroups.com
Hi Rick,

This is how I handle my login steps:

### login_steps.rb:

# log in as a default user
Given /^a user is logged in$/ do
  user = create_model
  visit new_user_session_path
  login_with_credentials(user.email, user.password)
end

# alias for “Given a user is logged in"
Given /^I am logged in$/ do
  Given “a user is logged in”
end

# login as a specified user
When /^I login as #{capture_model}$/ do |name|
  user = created_model!(name)  # created_model accesses password of existing object
  visit new_user_session_path   # go to the login page (which I suppose I could put into the helper instead)
  login_with_credentials(user.email, user.password)   # calls the helper below
end

# an alias for "When I login as ..."

Given /^#{capture_model} is logged in$/ do |name|
  When "I login as #{name}"
end

module LoginHelpers
  def login_with_credentials(email, password)
    fill_in('Email', :with => email)
    fill_in('Password', :with => password)
    click_button('Log in')
  end
end

World(LoginHelpers)

###

This lets me write steps like:

Given a user is logged in
When …
(when I just need to be logged in; I don’t care who the user is)

Given a user exists with email: “jo...@smith.com
When I login as that user
...

Given a user exists with name: “John Smith”
And that user is logged in

I try to avoid using instance variables across my steps. I use Pickle instead. I think the key in the above steps is the method created_model which allows you to access the password of the user that was created. If you just use model(‘user’) it reloads the object and you no longer have access to the password (just like you would normally when you find/reload a user object after creating it).

I hope that helps.

Cheers,

Michael.

Reply all
Reply to author
Forward
0 new messages