Post by Matt WynnePost by Rahul SharmaPost by Matt WynnePlease post your reply inline so that we keep the thread of the
conversation.
Thanks for posting inline this time! :)
Post by Rahul SharmaPost by Matt WynnePost by Rahul SharmaPost by Matt WynnePost by Rahul SharmaHi,
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 SharmaPost by Matt WynnePost by Rahul SharmaPost by Matt WynnePost by Rahul Sharmaabout 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?
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 SharmaPost by Matt WynnePost by Rahul SharmaPost by Matt WynneCan 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 SharmaPost by Matt WynnePost by Rahul SharmaHydra.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 SharmaPost by Matt WynnePost by Rahul SharmaDoes 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 Sharma1) 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 WynnePost by Rahul Sharma4) 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.