Discussion:
[Cucumber:9671] World object outside step definitions
(too old to reply)
Rahul Sharma
2011-11-11 11:33:55 UTC
Permalink
Raw Message
Hi,

I have defined a user object in the new world_extensions.rb file that
I have created. Now this works fine when I access this user object in
the step definitions but throws an error when I try to do the same in
one of my other modules in the lib folder(in the root of my project)
When I access the the user in this module I get the following error:
undefined method for nil class. It looks like even though the user
object has been instantiated in the World, it can not be accessed
outside step definitions or may be there is a way to extend it to know
about the lib modules? I am requiring all the files in the lib folder
through the env.rb before I define the World. Any help will be much
appreciated.

Cheers,

Rahul
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To post to this group, send email to cukes-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to cukes+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
Rahul Sharma
2011-11-11 14:45:10 UTC
Permalink
Raw Message
Sorry about the error message. I actually get an error saying
"undefined variable user for <name of the module/class in the lib
folder"
Post by Rahul Sharma
Hi,
I have defined a user object in the new world_extensions.rb file that
I have created. Now this works fine when I access this user object in
the step definitions but throws an error when I try to do the same in
one of my other modules in the lib folder(in the root of my project)
undefined method for nil class. It looks like even though the user
object has been instantiated in the World, it can not be accessed
outside step definitions or may be there is a way to extend it to know
about the lib modules? I am requiring all the files in the lib folder
through the env.rb before I define the World. Any help will be much
appreciated.
Cheers,
Rahul
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To post to this group, send email to cukes-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to cukes+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
Matt Wynne
2011-11-11 18:40:31 UTC
Permalink
Raw Message
Post by Rahul Sharma
Hi,
I have defined a user object in the new world_extensions.rb file that
I have created. Now this works fine when I access this user object in
the step definitions but throws an error when I try to do the same in
one of my other modules in the lib folder(in the root of my project)
undefined method for nil class. It looks like even though the user
object has been instantiated in the World, it can not be accessed
outside step definitions or may be there is a way to extend it to know
about the lib modules? I am requiring all the files in the lib folder
through the env.rb before I define the World. Any help will be much
appreciated.
Cheers,
Rahul
Are you just talking about an instance of a plain old ruby class, or an ActiveRecord object?

If you use an ActiveRecord object to create a row in a database, it should be possible to view it from both Cucumber and from your application. If you've instantiated a plain old Ruby class and stored that instance in a variable within your Cucumber World, you'll only be able to see that instance from within the World.

Can you explain more about what you're trying to achieve?

cheers,
Matt

--
Freelance programmer & coach
Author, http://pragprog.com/book/hwcuc/the-cucumber-book (with Aslak Hellesøy)
Founder, http://relishapp.com
+44(0)7974430184 | http://twitter.com/mattwynne
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To post to this group, send email to cukes-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to cukes+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
Rahul Sharma
2011-11-11 19:59:21 UTC
Permalink
Raw Message
It is an instance of normal Ruby class. We had quite a few calls to a user object in my step definitions and ruby modules that sit in a lib folder. In order to be able to use this we instansiate this for every usage. To eliminate this I have moved it to a world extension KnowsTheUser so that I could just call methods on the user without having to instantiate it. Now this object is known in the step definitions but not to the modules. For example I have a module that allows me to set certain values in a db using REST calls that take username as a parameter. So in that module if I do this:
Hydra.set_emailVerified(user.username) I get complained about user being an undefined variable as this module doesn't know about the user we instantiated in the world!!!

Does this make it clear what I am trying to achieve?

Kind Regards,

Rahul Sharma
Phone: +44 (0) 7800 736851
Post by Matt Wynne
Post by Rahul Sharma
Hi,
I have defined a user object in the new world_extensions.rb file that
I have created. Now this works fine when I access this user object in
the step definitions but throws an error when I try to do the same in
one of my other modules in the lib folder(in the root of my project)
undefined method for nil class. It looks like even though the user
object has been instantiated in the World, it can not be accessed
outside step definitions or may be there is a way to extend it to know
about the lib modules? I am requiring all the files in the lib folder
through the env.rb before I define the World. Any help will be much
appreciated.
Cheers,
Rahul
Are you just talking about an instance of a plain old ruby class, or an ActiveRecord object?
If you use an ActiveRecord object to create a row in a database, it should be possible to view it from both Cucumber and from your application. If you've instantiated a plain old Ruby class and stored that instance in a variable within your Cucumber World, you'll only be able to see that instance from within the World.
Can you explain more about what you're trying to achieve?
cheers,
Matt
--
Freelance programmer & coach
Author, http://pragprog.com/book/hwcuc/the-cucumber-book (with Aslak Hellesøy)
Founder, http://relishapp.com
+44(0)7974430184 | http://twitter.com/mattwynne
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To post to this group, send email to cukes-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to cukes+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
Matt Wynne
2011-11-11 21:26:10 UTC
Permalink
Raw Message
Please post your reply inline so that we keep the thread of the conversation.
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Hi,
I have defined a user object in the new world_extensions.rb file that
I have created. Now this works fine when I access this user object in
the step definitions but throws an error when I try to do the same in
one of my other modules in the lib folder(in the root of my project)
undefined method for nil class. It looks like even though the user
object has been instantiated in the World, it can not be accessed
outside step definitions or may be there is a way to extend it to know
about the lib modules? I am requiring all the files in the lib folder
through the env.rb before I define the World. Any help will be much
appreciated.
Cheers,
Rahul
Are you just talking about an instance of a plain old ruby class, or an ActiveRecord object?
If you use an ActiveRecord object to create a row in a database, it should be possible to view it from both Cucumber and from your application. If you've instantiated a plain old Ruby class and stored that instance in a variable within your Cucumber World, you'll only be able to see that instance from within the World.
Can you explain more about what you're trying to achieve?
Hydra.set_emailVerified(user.username) I get complained about user being an undefined variable as this module doesn't know about the user we instantiated in the world!!!
Does this make it clear what I am trying to achieve?
I'm still not clear, no. :)

Can you show us the scenario you're trying to automate? It would help to have some more context.

cheers,
Matt

--
Freelance programmer & coach
Author, http://pragprog.com/book/hwcuc/the-cucumber-book (with Aslak Hellesøy)
Founder, http://relishapp.com
+44(0)7974430184 | http://twitter.com/mattwynne
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To post to this group, send email to cukes-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to cukes+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
Rahul Sharma
2011-11-12 17:14:36 UTC
Permalink
Raw Message
Post by Matt Wynne
Please post your reply inline so that we keep the thread of the conversation.
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Hi,
I have defined a user object in the new world_extensions.rb file that
I have created. Now this works fine when I access this user object in
the step definitions but throws an error when I try to do the same in
one of my other modules in the lib folder(in the root of my project)
undefined method for nil class. It looks like even though the user
object has been instantiated in the World, it can not be accessed
outside step definitions or may be there is a way to extend it to know
about the lib modules? I am requiring all the files in the lib folder
through the env.rb before I define the World. Any help will be much
appreciated.
Cheers,
Rahul
Are you just talking about an instance of a plain old ruby class, or an ActiveRecord object?
If you use an ActiveRecord object to create a row in a database, it should be possible to view it from both Cucumber and from your application. If you've instantiated a plain old Ruby class and stored that instance in a variable within your Cucumber World, you'll only be able to see that instance from within the World.
Can you explain more about what you're trying to achieve?
Hydra.set_emailVerified(user.username) I get complained about user being an undefined variable as this module doesn't know about the user we instantiated in the world!!!
Does this make it clear what I am trying to achieve?
I'm still not clear, no. :)
Can you show us the scenario you're trying to automate? It would help to have some more context.
Sure, here is the scenario:

1) Given I am on the homepage
2) When I register
3) And I verify my email address
4) When I visit the homepage
5) Then I should not be prompted to verify my email address

I don't have the step definition code handy but here is the pseudo code:

1) visit Homepage

2) do_registration(user)
# user is defined in a World module as below
def user
@user = User.new
end

3) hydra.verify_email_address(user.username) # This is what causes the error. hydra is also something I have instantiated in the world just like the user. This verify_email_address method comes from a module in the lib folder and doesn't know about the user parameter. Hence I get user undefined variable error!!

4) visit Homepage

5) verify_email_address_message should_not exist
Post by Matt Wynne
cheers,
Matt
--
Freelance programmer & coach
Author, http://pragprog.com/book/hwcuc/the-cucumber-book (with Aslak Hellesøy)
Founder, http://relishapp.com
+44(0)7974430184 | http://twitter.com/mattwynne
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
Regards,
Rahul Sharma
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To post to this group, send email to cukes-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to cukes+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
Matt Wynne
2011-11-14 16:10:49 UTC
Permalink
Raw Message
Post by Rahul Sharma
Post by Matt Wynne
Please post your reply inline so that we keep the thread of the conversation.
Thanks for posting inline this time! :)
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Hi,
I have defined a user object in the new world_extensions.rb file that
I have created. Now this works fine when I access this user object in
the step definitions but throws an error when I try to do the same in
one of my other modules in the lib folder(in the root of my project)
undefined method for nil class. It looks like even though the user
object has been instantiated in the World, it can not be accessed
outside step definitions or may be there is a way to extend it to know
about the lib modules? I am requiring all the files in the lib folder
through the env.rb before I define the World. Any help will be much
appreciated.
Cheers,
Rahul
Are you just talking about an instance of a plain old ruby class, or an ActiveRecord object?
If you use an ActiveRecord object to create a row in a database, it should be possible to view it from both Cucumber and from your application. If you've instantiated a plain old Ruby class and stored that instance in a variable within your Cucumber World, you'll only be able to see that instance from within the World.
Can you explain more about what you're trying to achieve?
Hydra.set_emailVerified(user.username) I get complained about user being an undefined variable as this module doesn't know about the user we instantiated in the world!!!
Does this make it clear what I am trying to achieve?
I'm still not clear, no. :)
Can you show us the scenario you're trying to automate? It would help to have some more context.
1) Given I am on the homepage
2) When I register
3) And I verify my email address
4) When I visit the homepage
5) Then I should not be prompted to verify my email address
1) visit Homepage
2) do_registration(user)
# user is defined in a World module as below
def user
@user = User.new
end
3) hydra.verify_email_address(user.username) # This is what causes the error. hydra is also something I have instantiated in the world just like the user. This verify_email_address method comes from a module in the lib folder and doesn't know about the user parameter. Hence I get user undefined variable error!!
Are you saying that you call this line from a method in World? That should work. If you've defined the user method in step (2) in World, then you should be able to call it from anywhere in your step definitions.

It would be much easier to help if you can post the relevant parts of the actual code. Can you do that please?
Post by Rahul Sharma
4) visit Homepage
5) verify_email_address_message should_not exist
cheers,
Matt

--
Freelance programmer & coach
Author, http://pragprog.com/book/hwcuc/the-cucumber-book (with Aslak Hellesøy)
Founder, http://relishapp.com
+44(0)7974430184 | http://twitter.com/mattwynne
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To post to this group, send email to cukes-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to cukes+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
Rahul Sharma
2011-11-14 17:50:04 UTC
Permalink
Raw Message
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
Please post your reply inline so that we keep the thread of the
conversation.
Thanks for posting inline this time! :)
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Hi,
I have defined a user object in the new world_extensions.rb file that
I have created. Now this works fine when I access this user object in
the step definitions but throws an error when I try to do the same in
one of my other modules in the lib folder(in the root of my project)
undefined method for nil class. It looks like even though the user
object has been instantiated in the World, it can not be accessed
outside step definitions or may be there is a way to extend it to
know
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
about the lib modules? I am requiring all the files in the lib folder
through the env.rb before I define the World. Any help will be much
appreciated.
Cheers,
Rahul
Are you just talking about an instance of a plain old ruby class, or
an ActiveRecord object?
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
If you use an ActiveRecord object to create a row in a database, it
should be possible to view it from both Cucumber and from your application.
If you've instantiated a plain old Ruby class and stored that instance in a
variable within your Cucumber World, you'll only be able to see that
instance from within the World.
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
Can you explain more about what you're trying to achieve?
It is an instance of normal Ruby class. We had quite a few calls to a
user object in my step definitions and ruby modules that sit in a lib
folder. In order to be able to use this we instansiate this for every
usage. To eliminate this I have moved it to a world extension KnowsTheUser
so that I could just call methods on the user without having to instantiate
it. Now this object is known in the step definitions but not to the
modules. For example I have a module that allows me to set certain values
in a db using REST calls that take username as a parameter. So in that
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Hydra.set_emailVerified(user.username) I get complained about user
being an undefined variable as this module doesn't know about the user we
instantiated in the world!!!
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Does this make it clear what I am trying to achieve?
I'm still not clear, no. :)
Can you show us the scenario you're trying to automate? It would help
to have some more context.
Post by Rahul Sharma
1) Given I am on the homepage
2) When I register
3) And I verify my email address
4) When I visit the homepage
5) Then I should not be prompted to verify my email address
1) visit Homepage
2) do_registration(user)
# user is defined in a World module as below
def user
@user = User.new
end
3) hydra.verify_email_address(user.username) # This is what causes the
error. hydra is also something I have instantiated in the world just like
the user. This verify_email_address
method comes from a module in the lib folder and doesn't know about the
user parameter. Hence I get user undefined variable error!!
Are you saying that you call this line from a method in World? That should
work. If you've defined the user method in step (2) in World, then you
should be able to call it from anywhere in your step definitions.
It would be much easier to help if you can post the relevant parts of the
actual code. Can you do that please?
ok to avoid confusion. Here is what I have got.

My world_extensions.rb file:

module KnowsTheUser

def user

@user ||= Common::Models::User.new

end

end


module KnowsHydraConfig

def hydra

@hydra ||= Common::Utils::Hydra::Hydra.new

end

end


World(KnowsTheUser,KnowsHydraConfig)

I have no other method defined in the World other than just creating a new
object. Both @hydra & @user are objects of Hydra and User class
respectively that sit in the lib directory like this:

module Common
module Models
class User
attr_accessor :username
#user constructor and other methods here
end
end
end


module Common
module Utils
module Hydra
class Hydra
# Hydra constructor and other methods here. Also contains this
method:
def set_email_verified(username)
# code to verify email address
end
end
end
end
end

Hence I use hydra(defined in the World above) to call the method in my step
definition
hydra.set_email_verified(user.username) # The argument passed in is not
picked up and I see user as an undefined variable. I was hoping that since
user is now defined in the World it would know what I am referring to. I
solved it by instantiating the user again in the method but I guess I
shouldn't have to do it as this is already done before the scenario through
the World.

May be I am missing something !!
Post by Matt Wynne
Post by Rahul Sharma
4) visit Homepage
5) verify_email_address_message should_not exist
cheers,
Matt
--
Freelance programmer & coach
Author, http://pragprog.com/book/hwcuc/the-cucumber-book (with Aslak Hellesøy)
Founder, http://relishapp.com
+44(0)7974430184 | http://twitter.com/mattwynne
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/cukes?hl=en.
--
Regards,

Rahul Sharma
Ph:+44 7800 736851
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To post to this group, send email to cukes-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to cukes+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
Andrew Premdas
2011-11-15 08:55:51 UTC
Permalink
Raw Message
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
Please post your reply inline so that we keep the thread of the conversation.
Thanks for posting inline this time! :)
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Hi,
I have defined a user object in the new world_extensions.rb file that
I have created. Now this works fine when I access this user object in
the step definitions but throws an error when I try to do the same in
one of my other modules in the lib folder(in the root of my project)
undefined method for nil class. It looks like even though the user
object has been instantiated in the World, it can not be accessed
outside step definitions or may be there is a way to extend it to know
about the lib modules? I am requiring all the files in the lib folder
through the env.rb before I define the World. Any help will be much
appreciated.
Cheers,
Rahul
Are you just talking about an instance of a plain old ruby class, or
an ActiveRecord object?
If you use an ActiveRecord object to create a row in a database, it
should be possible to view it from both Cucumber and from your application.
If you've instantiated a plain old Ruby class and stored that instance in a
variable within your Cucumber World, you'll only be able to see that
instance from within the World.
Can you explain more about what you're trying to achieve?
It is an instance of normal Ruby class. We had quite a few calls to a
user object in my step definitions and ruby modules that sit in a lib
folder. In order to be able to use this we instansiate this for every usage.
To eliminate this I have moved it to a world extension KnowsTheUser so that
I could just call methods on the user without having to instantiate it. Now
this object is known in the step definitions but not to the modules. For
example I have a module that allows me to set certain values in a db using
REST calls that take username as a parameter. So in that module if I do
Hydra.set_emailVerified(user.username) I get complained about user
being an undefined variable as this module doesn't know about the user we
instantiated in the world!!!
Does this make it clear what I am trying to achieve?
I'm still not clear, no. :)
Can you show us the scenario you're trying to automate? It would help
to have some more context.
1) Given I am on the homepage
2) When I register
3) And I verify my email address
4) When I visit the homepage
5) Then I should not be prompted to verify my email address
I don't have the step definition code handy but here is the pseudo
1) visit Homepage
2) do_registration(user)
# user is defined in a World module as below
def user
end
3) hydra.verify_email_address(user.username) # This is what causes the
error. hydra is also something I have instantiated in the world just like
the user. This verify_email_address
 method comes from a module in the lib folder and doesn't know about the
user parameter. Hence I get user undefined variable error!!
Are you saying that you call this line from a method in World? That should
work. If you've defined the user method in step (2) in World, then you
should be able to call it from anywhere in your step definitions.
It would be much easier to help if you can post the relevant parts of the
actual code. Can you do that please?
ok to avoid confusion. Here is what I have got.
module KnowsTheUser
  def user
  end
end
module KnowsHydraConfig
  def hydra
  end
end
World(KnowsTheUser,KnowsHydraConfig)
I have no other method defined in the World other than just creating a new
module Common
  module Models
    class User
       attr_accessor :username
      #user constructor and other methods here
    end
  end
end
module Common
  module Utils
   module Hydra
    class Hydra
       # Hydra constructor and other methods here. Also contains this
            def set_email_verified(username)
                # code to verify email address
            end
    end
   end
  end
end
Hence I use hydra(defined in the World above) to call the method in my step
definition
    hydra.set_email_verified(user.username) # The argument passed in is not
picked up and I see user as an undefined variable. I was hoping that since
user is now defined in the World it would know what I am referring to. I
 solved it by instantiating the user again in the method but I guess I
shouldn't have to do it as this is already done before the scenario through
the World.
May be I am missing something !!
Post by Matt Wynne
Post by Rahul Sharma
4) visit Homepage
5) verify_email_address_message should_not exist
cheers,
Matt
--
Freelance programmer & coach
Author, http://pragprog.com/book/hwcuc/the-cucumber-book (with Aslak Hellesøy)
Founder, http://relishapp.com
+44(0)7974430184 | http://twitter.com/mattwynne
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/cukes?hl=en.
--
Regards,
Rahul Sharma
Ph:+44 7800 736851
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/cukes?hl=en.
I'm not completing following this, but I think I've had similar
issues. The thing to do is debug where you are having problems and
look at self. CucumberWorld is a bit of magic that exists only in step
definition code. As soon as you write class XXX then the world is lost
(this is actually a good thing) as self changes. You should definitely
not try and solve this by passing in CucumberWorld, instead change the
design of your classes so they don't call stuff they can't reach.

HTH

Andrew
--
------------------------
Andrew Premdas
blog.andrew.premdas.org
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To post to this group, send email to cukes-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to cukes+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
Matt Wynne
2011-11-15 11:13:22 UTC
Permalink
Raw Message
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
Please post your reply inline so that we keep the thread of the conversation.
Thanks for posting inline this time! :)
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Hi,
I have defined a user object in the new world_extensions.rb file that
I have created. Now this works fine when I access this user object in
the step definitions but throws an error when I try to do the same in
one of my other modules in the lib folder(in the root of my project)
undefined method for nil class. It looks like even though the user
object has been instantiated in the World, it can not be accessed
outside step definitions or may be there is a way to extend it to know
about the lib modules? I am requiring all the files in the lib folder
through the env.rb before I define the World. Any help will be much
appreciated.
Cheers,
Rahul
Are you just talking about an instance of a plain old ruby class, or an ActiveRecord object?
If you use an ActiveRecord object to create a row in a database, it should be possible to view it from both Cucumber and from your application. If you've instantiated a plain old Ruby class and stored that instance in a variable within your Cucumber World, you'll only be able to see that instance from within the World.
Can you explain more about what you're trying to achieve?
Hydra.set_emailVerified(user.username) I get complained about user being an undefined variable as this module doesn't know about the user we instantiated in the world!!!
Does this make it clear what I am trying to achieve?
I'm still not clear, no. :)
Can you show us the scenario you're trying to automate? It would help to have some more context.
1) Given I am on the homepage
2) When I register
3) And I verify my email address
4) When I visit the homepage
5) Then I should not be prompted to verify my email address
1) visit Homepage
2) do_registration(user)
# user is defined in a World module as below
def user
@user = User.new
end
3) hydra.verify_email_address(user.username) # This is what causes the error. hydra is also something I have instantiated in the world just like the user. This verify_email_address method comes from a module in the lib folder and doesn't know about the user parameter. Hence I get user undefined variable error!!
Are you saying that you call this line from a method in World? That should work. If you've defined the user method in step (2) in World, then you should be able to call it from anywhere in your step definitions.
It would be much easier to help if you can post the relevant parts of the actual code. Can you do that please?
ok to avoid confusion. Here is what I have got.
module KnowsTheUser
def user
@user ||= Common::Models::User.new
end
end
module KnowsHydraConfig
def hydra
@hydra ||= Common::Utils::Hydra::Hydra.new
end
end
World(KnowsTheUser,KnowsHydraConfig)
module Common
module Models
class User
attr_accessor :username
#user constructor and other methods here
end
end
end
module Common
module Utils
module Hydra
class Hydra
def set_email_verified(username)
# code to verify email address
end
end
end
end
end
Hence I use hydra(defined in the World above) to call the method in my step definition
hydra.set_email_verified(user.username) # The argument passed in is not picked up and I see user as an undefined variable. I was hoping that since user is now defined in the World it would know what I am referring to. I solved it by instantiating the user again in the method but I guess I shouldn't have to do it as this is already done before the scenario through the World.
May be I am missing something !!
Yes, what you're expecting should work. I can't see why it isn't. Without looking at the actual code I'm not sure we can get any further, sorry.
Post by Matt Wynne
Post by Rahul Sharma
4) visit Homepage
5) verify_email_address_message should_not exist
cheers,
Matt
--
Freelance programmer & coach
Author, http://pragprog.com/book/hwcuc/the-cucumber-book (with Aslak Hellesøy)
Founder, http://relishapp.com
+44(0)7974430184 | http://twitter.com/mattwynne
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
--
Regards,
Rahul Sharma
Ph:+44 7800 736851
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
cheers,
Matt

--
Freelance programmer & coach
Author, http://pragprog.com/book/hwcuc/the-cucumber-book (with Aslak Hellesøy)
Founder, http://relishapp.com
+44(0)7974430184 | http://twitter.com/mattwynne
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To post to this group, send email to cukes-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to cukes+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
Kruthi
2018-03-13 00:09:29 UTC
Permalink
Raw Message
Hi Rahul,

Did you find any solution for your problem? Im facing similar issues. Can
you please post your solution it would really help me?

Thanks,
Kruthi
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
Please post your reply inline so that we keep the thread of the
conversation.
Thanks for posting inline this time! :)
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Hi,
I have defined a user object in the new world_extensions.rb file
that
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
I have created. Now this works fine when I access this user object
in
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
the step definitions but throws an error when I try to do the same
in
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
one of my other modules in the lib folder(in the root of my project)
undefined method for nil class. It looks like even though the user
object has been instantiated in the World, it can not be accessed
outside step definitions or may be there is a way to extend it to
know
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
about the lib modules? I am requiring all the files in the lib
folder
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
through the env.rb before I define the World. Any help will be much
appreciated.
Cheers,
Rahul
Are you just talking about an instance of a plain old ruby class, or
an ActiveRecord object?
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
If you use an ActiveRecord object to create a row in a database, it
should be possible to view it from both Cucumber and from your application.
If you've instantiated a plain old Ruby class and stored that instance in a
variable within your Cucumber World, you'll only be able to see that
instance from within the World.
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Post by Matt Wynne
Can you explain more about what you're trying to achieve?
It is an instance of normal Ruby class. We had quite a few calls to a
user object in my step definitions and ruby modules that sit in a lib
folder. In order to be able to use this we instansiate this for every
usage. To eliminate this I have moved it to a world extension KnowsTheUser
so that I could just call methods on the user without having to instantiate
it. Now this object is known in the step definitions but not to the
modules. For example I have a module that allows me to set certain values
in a db using REST calls that take username as a parameter. So in that
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Hydra.set_emailVerified(user.username) I get complained about user
being an undefined variable as this module doesn't know about the user we
instantiated in the world!!!
Post by Rahul Sharma
Post by Matt Wynne
Post by Rahul Sharma
Does this make it clear what I am trying to achieve?
I'm still not clear, no. :)
Can you show us the scenario you're trying to automate? It would help
to have some more context.
Post by Rahul Sharma
1) Given I am on the homepage
2) When I register
3) And I verify my email address
4) When I visit the homepage
5) Then I should not be prompted to verify my email address
I don't have the step definition code handy but here is the pseudo
1) visit Homepage
2) do_registration(user)
# user is defined in a World module as below
def user
@user = User.new
end
3) hydra.verify_email_address(user.username) # This is what causes the
error. hydra is also something I have instantiated in the world just like
the user. This verify_email_address
method comes from a module in the lib folder and doesn't know about the
user parameter. Hence I get user undefined variable error!!
Are you saying that you call this line from a method in World? That
should work. If you've defined the user method in step (2) in World, then
you should be able to call it from anywhere in your step definitions.
It would be much easier to help if you can post the relevant parts of the
actual code. Can you do that please?
ok to avoid confusion. Here is what I have got.
module KnowsTheUser
def user
@user ||= Common::Models::User.new
end
end
module KnowsHydraConfig
def hydra
@hydra ||= Common::Utils::Hydra::Hydra.new
end
end
World(KnowsTheUser,KnowsHydraConfig)
I have no other method defined in the World other than just creating a new
module Common
module Models
class User
attr_accessor :username
#user constructor and other methods here
end
end
end
module Common
module Utils
module Hydra
class Hydra
# Hydra constructor and other methods here. Also contains this
def set_email_verified(username)
# code to verify email address
end
end
end
end
end
Hence I use hydra(defined in the World above) to call the method in my
step definition
hydra.set_email_verified(user.username) # The argument passed in is
not picked up and I see user as an undefined variable. I was hoping that
since user is now defined in the World it would know what I am referring
to. I solved it by instantiating the user again in the method but I guess
I shouldn't have to do it as this is already done before the scenario
through the World.
May be I am missing something !!
Post by Matt Wynne
Post by Rahul Sharma
4) visit Homepage
5) verify_email_address_message should_not exist
cheers,
Matt
--
Freelance programmer & coach
Author, http://pragprog.com/book/hwcuc/the-cucumber-book (with Aslak
HellesÞy)
Founder, http://relishapp.com
+44(0)7974430184 | http://twitter.com/mattwynne
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/cukes?hl=en.
--
Regards,
Rahul Sharma
Ph:+44 7800 736851
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Anthony Green
2011-11-13 18:59:55 UTC
Permalink
Raw Message
Post by Rahul Sharma
Hydra.set_emailVerified(user.username) I get complained about user being an undefined variable as this module doesn't know about the user we instantiated in the world!!!
Does this make it clear what I am trying to achieve?
I'm going to take a guess.

You need to create a User class and a separate UserWorld Module that
provides methods for step definitions to access an instance of that
class and mix the module into the World.

Your issue could be a either of being in the wrong scope or because
your class is effectively name spaced inside the module but without
the code I can't tell.

A link to a Gist is probably the best way we can work out what's
wrong.

Best

Tony
--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To post to this group, send email to cukes-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to cukes+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
Loading...