capture_fields question

33 views
Skip to first unread message

millisami

unread,
Jun 23, 2010, 5:26:11 AM6/23/10
to pickle
Hi, I'm here again with a simplified question for the thread
http://groups.google.com/group/pickle-cucumber/browse_thread/thread/52c43d61b0177255

My experience with pickle is decent and I have FactoryGirl setup and
I've setup custom steps like:
Given I am logged in as vendor admin via custom Factory methods.

The real problem is with the pickle's capture_fields method.
User has many roles and this is not setup as association. Is setup as
bit mask. I've roles_mask column with integer type in the database and
the roles getter and setter method is set to deal with the conversion.

I know that capture_model does its job is through the reflection of
model. Since the User model has its attribute column named as
roles_mask, can this be a problem?

But looking at the errors, it seems like the roles= setter method is
the problem.
When I change the roles=(roles) method to the commented one below, it
passes.

First the User model
class User < ActiveRecord::Base
#if new role is needed, then insert at the last, NOT IN THE MIDDLE
ROLES = %w[super_admin vendor_admin client_admin
client_trade_officer]

def roles=(roles)
self.roles_mask = (roles & ROLES).map { |r|
2**ROLES.index(r) }.sum
end

# def roles=(roles)
# self.roles_mask = 2 #or any integer
# end

def roles
ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }
end
end

Here is the feature:
@user-roles
Scenario: User with roles
Given a user exists
And another user exists with roles: "vendor_admin"

Here the error:
@user-roles
Scenario: User with roles # features/
campaigns.feature:31
Given a user exists # features/
step_definitions/pickle_steps.rb:4
And another user exists with roles: "vendor_admin" # features/
step_definitions/pickle_steps.rb:4
undefined method `&' for "vendor_admin":String (NoMethodError)
./app/models/user.rb:18:in `roles='

Any direction or tips coz this is stopping me to go ahead with my
cucumber features? I need it to pass badly!!

Ian White

unread,
Jun 23, 2010, 5:57:28 AM6/23/10
to pickle-...@googlegroups.com
Hi there,

I don't think capture_fields is the problem tbh. The problem is just that the roles= setter expects an array but pickle doesn't how to do that (yet).

There are two solutions that I can see to your problem, using current pickle. One is to rewrite your roles= method to convert string to an array.

def roles=(roles)
roles = roles.split(',').map(&:squish) unless roles.is_a?(Array)
...
end

this will convert the arg as follows:

'vendor_admin' => ['vendor_admin']
'vendor_admin, client_admin' => ['vendor_admin', 'client_admin']

Or, write a custom step something like (remember pickle_steps.rb is just a starting point)

Given /a #{capture_model} with role: (/w+) exists/ do |model, role|
user = create_model(model)
user.roles = [role]
end

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

millisami

unread,
Jun 23, 2010, 6:12:46 AM6/23/10
to pickle
I forgot to tell that I've migrated from this thread
http://groups.google.com/group/pickle-cucumber/browse_thread/thread/52c43d61b0177255
coz that was messed up with google's only support for text.


On Jun 23, 2:26 pm, millisami <millis...@gmail.com> wrote:
> Hi, I'm here again with a simplified question for the threadhttp://groups.google.com/group/pickle-cucumber/browse_thread/thread/5...

millisami

unread,
Jun 23, 2010, 6:36:26 AM6/23/10
to pickle
Yes, this is what I want and I tried it, and I test it, it gives me
regx errors. Tried it to resolve, but the regex error persists.
I'm trying this with the second method.

The feature I am using is
Given a user with role: "vendor_admin" exists

ree-1.8.7-2010.01 [~/rails_apps/automation (pyrite)⚡] ➔ bundle exec
cucumber features/campaigns.feature -t @user-roles -f pretty
Using the default profile...
/Users/millisami/rails_apps/automation/features/step_definitions/
user_session_steps.rb:29: unknown regexp option - w
/Users/millisami/rails_apps/automation/features/step_definitions/
user_session_steps.rb:29: syntax error, unexpected ')'
Given /a #{capture_model} with role: (/w+) exists/ do |model, role|
^
/Users/millisami/rails_apps/automation/features/step_definitions/
user_session_steps.rb:29: syntax error, unexpected '|', expecting '='
Given /a #{capture_model} with role: (/w+) exists/ do |model, role|
^
/Users/millisami/rails_apps/automation/features/step_definitions/
user_session_steps.rb:32: syntax error, unexpected kEND, expecting
$end (SyntaxError)


On Jun 23, 2:57 pm, Ian White <ian.w.wh...@gmail.com> wrote:
> Hi there,
>
> I don't think capture_fields is the problem tbh.  The problem is just that the roles= setter expects an array but pickle doesn't how to do that (yet).
>
> There are two solutions that I can see to your problem, using current pickle.  One is to rewrite your roles= method to convert string to an array.
>
> def roles=(roles)
>   roles = roles.split(',').map(&:squish) unless roles.is_a?(Array)
>   ...
> end
>
> this will convert the arg as follows:
>
>   'vendor_admin' => ['vendor_admin']
>   'vendor_admin, client_admin' => ['vendor_admin', 'client_admin']
>
> Or, write a custom step something like (remember pickle_steps.rb is just a starting point)
>
> Given /a #{capture_model} with role: (/w+) exists/ do |model, role|
>   user = create_model(model)
>   user.roles = [role]
> end
>
> Hope that helps
>
> Cheers,
> Ian
>
> On 23 Jun 2010, at 10:26, millisami wrote:
>
>
>
> > Hi, I'm here again with a simplified question for the thread
> >http://groups.google.com/group/pickle-cucumber/browse_thread/thread/5...

Ian White

unread,
Jun 23, 2010, 6:43:13 AM6/23/10
to pickle-...@googlegroups.com
In your regexp, the /w should be a \w

Cheers,
Ian

millisami

unread,
Jun 23, 2010, 7:07:56 AM6/23/10
to pickle
Well I changed that and now its different error. It tries to find that
roles= method on Array?

@user-roles
Scenario: User with roles # features/
campaigns.feature:31
Given a user with role: vendor_admin exists # features/
step_definitions/user_session_steps.rb:29
undefined method `roles=' for #<Array:0x1038dc8a0>
(NoMethodError)


Step def:
Given /a #{capture_model} with role: (\w+) exists/ do |model, role|
user = create_model(model)
user.roles = [role]
end


Ian White

unread,
Jun 23, 2010, 7:13:53 AM6/23/10
to pickle-...@googlegroups.com
SOrry create_model doesn't do what you might expect...

Try this

create_model(model)
model!(model).roles = [role]

millisami

unread,
Jun 23, 2010, 7:17:27 AM6/23/10
to pickle
Doing that now its a different error:
@user-roles
Scenario: User with roles # features/
campaigns.feature:31
Given a user with role: vendor_admin exists # features/
step_definitions/user_session_steps.rb:29
can't convert Array into String (TypeError)

Ian White

unread,
Jun 23, 2010, 7:19:51 AM6/23/10
to pickle-...@googlegroups.com
Can you post more stacktrace please. Or use the debugger to see what's going on.

Cheers,
Ian

millisami

unread,
Jun 23, 2010, 7:27:57 AM6/23/10
to pickle

Ian White

unread,
Jun 23, 2010, 7:44:07 AM6/23/10
to pickle-...@googlegroups.com
Can you please post user_steps.rb as well

millisami

unread,
Jun 23, 2010, 7:56:49 AM6/23/10
to pickle
Here it is:
Given /a #{capture_model} with role: (\w+) exists/ do |model, role|
user = create_model(model)
model!(user).roles = [role]
end

Ian White

unread,
Jun 23, 2010, 7:57:43 AM6/23/10
to pickle-...@googlegroups.com
Could you tell me the line numbers please

millisami

unread,
Jun 23, 2010, 8:12:40 AM6/23/10
to pickle

Ian White

unread,
Jun 23, 2010, 8:23:40 AM6/23/10
to pickle-...@googlegroups.com
This one's stumping me a bit. Could you try using the following regexp, just for kicks...

Given(/^#{capture_model} exists with role: (\w+)$/ do |user, role|
create_model(user)
model!(user).roles = [role]
end

millisami

unread,
Jun 23, 2010, 8:29:47 AM6/23/10
to pickle
With that regx, its reg error again:

ree-1.8.7-2010.01 [~/rails_apps/automation (pyrite)⚡] ➔ bundle exec
cucumber features/campaigns.feature -t @user-roles -f pretty
Using the default profile...
/Users/millisami/rails_apps/automation/features/step_definitions/
user_session_steps.rb:33: syntax error, unexpected kDO, expecting ')'
Given(/^#{capture_model} exists with role: (\w+)$/ do |user, role|
^
/Users/millisami/rails_apps/automation/features/step_definitions/
user_session_steps.rb:33: syntax error, unexpected '|', expecting '='
Given(/^#{capture_model} exists with role: (\w+)$/ do |user, role|
^
/Users/millisami/rails_apps/automation/features/step_definitions/
user_session_steps.rb:36: syntax error, unexpected kEND, expecting
$end (SyntaxError)
/Users/millisami/.rvm/gems/ree-1.8.7-2010.01/gems/activesupport-2.3.8/
lib/active_support/dependencies.rb:158:in `require'
/Users/millisami/.rvm/gems/ree-1.8.7-2010.01/gems/activesupport-2.3.8/
lib/active_support/dependencies.rb:158:in `require'
/Users/millisami/.rvm/gems/ree-1.8.7-2010.01/gems/cucumber-0.6.4/
bin/../lib/cucumber/rb_support/rb_language.rb:124:in `load_code_file'
/Users/millisami/.rvm/gems/ree-1.8.7-2010.01/gems/cucumber-0.6.4/
bin/../lib/cucumber/step_mother.rb:85:in `load_code_file'
/Users/millisami/.rvm/gems/ree-1.8.7-2010.01/gems/cucumber-0.6.4/
bin/../lib/cucumber/step_mother.rb:77:in `load_code_files'
/Users/millisami/.rvm/gems/ree-1.8.7-2010.01/gems/cucumber-0.6.4/
bin/../lib/cucumber/step_mother.rb:76:in `each'
/Users/millisami/.rvm/gems/ree-1.8.7-2010.01/gems/cucumber-0.6.4/
bin/../lib/cucumber/step_mother.rb:76:in `load_code_files'
/Users/millisami/.rvm/gems/ree-1.8.7-2010.01/gems/cucumber-0.6.4/
bin/../lib/cucumber/cli/main.rb:51:in `execute!'
/Users/millisami/.rvm/gems/ree-1.8.7-2010.01/gems/cucumber-0.6.4/
bin/../lib/cucumber/cli/main.rb:20:in `execute'
/Users/millisami/.rvm/gems/ree-1.8.7-2010.01/gems/cucumber-0.6.4/bin/
cucumber:8
/Users/millisami/.rvm/gems/ree-1.8.7-2010.01/bin/cucumber:19:in `load'
/Users/millisami/.rvm/gems/ree-1.8.7-2010.01/bin/cucumber:19


On Jun 23, 5:23 pm, Ian White <ian.w.wh...@gmail.com> wrote:
> This one's stumping me a bit.  Could you try using the following regexp, just for kicks...
>
> Given(/^#{capture_model} exists with role: (\w+)$/ do |user, role|
>   create_model(user)
>   model!(user).roles = [role]
> end
>
> On 23 Jun 2010, at 13:12, millisami wrote:
>
>
>
> > Here it is.http://gist.github.com/449847
> ...
>
> read more »

Ian White

unread,
Jun 23, 2010, 8:46:54 AM6/23/10
to pickle-...@googlegroups.com
Sorry list for the spam, just realised I was posting on list, taking this off list.

Cheers,
Ian

Sent from my iPhone

millisami

unread,
Jun 23, 2010, 8:55:13 AM6/23/10
to pickle
What might be the error with that regex?

On Jun 23, 5:46 pm, Ian White <ian.w.wh...@gmail.com> wrote:
> Sorry list for the spam, just realised I was posting on list, taking this off list.
>
> Cheers,
> Ian
>
> Sent from my iPhone
>
> ...
>
> read more »

Ian White

unread,
Jun 23, 2010, 9:03:18 AM6/23/10
to pickle-...@googlegroups.com
Hi I sent you a reply off list, best to reply to that email.

CHeers,
Ian

millisami

unread,
Jun 23, 2010, 9:35:05 AM6/23/10
to pickle
Even I changed that, its still the same regex error.
Given(/^a #{capture_model} exists with role: (\w+)$/ do |user, role|
create_model(user)
model!(user).roles = [role]
end

@user-roles
Scenario: User with roles
Given a user exists with role: vendor_admin

The error:
ree-1.8.7-2010.01 [~/rails_apps/automation (pyrite)⚡] ➔ bundle exec
cucumber features/campaigns.feature -t @user-roles -f pretty
Using the default profile...
/Users/millisami/rails_apps/automation/features/step_definitions/
user_session_steps.rb:33: syntax error, unexpected kDO, expecting ')'
Given(/^a #{capture_model} exists with role: (\w+)$/ do |user, role|
^
/Users/millisami/rails_apps/automation/features/step_definitions/
user_session_steps.rb:33: syntax error, unexpected '|', expecting '='
Given(/^a #{capture_model} exists with role: (\w+)$/ do |user, role|
^
/Users/millisami/rails_apps/automation/features/step_definitions/
user_session_steps.rb:36: syntax error, unexpected kEND, expecting
$end (SyntaxError)


> ...
>
> read more »

Ian White

unread,
Jun 23, 2010, 10:32:33 AM6/23/10
to pickle-...@googlegroups.com
Given(/^a #{capture_model} exists with role: (\w+)$/) do |user, role|

Sent from my iPhone

Michael MacDonald

unread,
Jun 23, 2010, 11:25:17 AM6/23/10
to pickle-...@googlegroups.com
So the answer was a missing ) for the Given and not setting user to the output of create_model(user). I’d also remove the “a” at the start since that’s taken care of by capture_model: given a user, given the user, given the first user etc.

Sachin Sagar Rai

unread,
Jun 23, 2010, 11:35:01 AM6/23/10
to pickle-...@googlegroups.com
Yup, the closing ) was the error and also the (\w+)
Since, the step_def is similar if I use 

Given a user exists with role: vendor_admin

I get the following ambiguous error
  @user-roles
  Scenario: User with roles                     # features/campaigns.feature:31
    Given a user exists with role: vendor_admin # features/campaigns.feature:32
      Ambiguous match of "a user exists with role: vendor_admin":

So, I nailed it down with this version
Given a user with role: vendor_admin exists

Thanks a lot Ian for this continuous help.

Sachin Sagar Rai (aka - millisami)

Ian White

unread,
Jun 23, 2010, 12:03:01 PM6/23/10
to pickle-...@googlegroups.com
Glad it's sorted. Apologies for distracted iPhone England watching code musings...  Although it would have been worse in 2 hours when Australia are playing!

Aussie Aussie Aussie!

Sent from my iPhone
Reply all
Reply to author
Forward
0 new messages