Homemade Shoulda Macros

0 views
Skip to first unread message

Christopher Gardner

unread,
Sep 25, 2009, 9:13:34 PM9/25/09
to sho...@googlegroups.com
I'm fairly new to Ruby so please help me blot out my ignorance. I
find that occasionally I'll need to reuse a should for different
contexts. So I create a corresponding method, but I have to create it
ahead of the class definition. Why do I have to define my should
method outside the class? Moreover, in the method I can refer to
private variables and methods inside the class. I know this must have
something about how Ruby allocates and initializes objects. Thanks.

require 'test/unit'
require 'shoulda'

def should_say_hello
should "say hello" do
puts "should say hello"
check
end
end

class ShouldaTest < Test::Unit::TestCase

def initialize(suite)
super(suite)
puts "->>>>>>>>>>>> Initialize <<<<<<<<<-"
end

context "An new object" do
setup do
puts 'context'
@hw = HelloWorld.new
end

should_say_hello
end

private
def check
assert_equal('hello', @hw.greet)
end
end

class HelloWorld
def initialize
puts 'hello world created'
end
def greet
'hello'
end
end

Chris Gardner

unread,
Sep 25, 2009, 10:26:36 PM9/25/09
to shoulda
I guess I shouldn't have called the post "macros"

On Sep 25, 9:13 pm, Christopher Gardner <chris.r.gard...@gmail.com>
wrote:

david.l...@gmail.com

unread,
Sep 26, 2009, 2:41:32 PM9/26/09
to sho...@googlegroups.com
Hi Chris,
You don't have to define the method in the Object space outside of
the class.
But it needs to be defined as a class method like so:

class ShouldaTest < Test::Unit::TestCase
def self.should_say_hello
...
end
end

This is because the should methods are called while the TestCase class
is being interpreted, and not later when the class is instantiated.


-David

Christopher Gardner

unread,
Sep 26, 2009, 2:57:08 PM9/26/09
to sho...@googlegroups.com
Thanks, David.

Now in

def self.should_say_hello
should "say hello" do
check_hello_world
end
end

Now when I call should_say_hello in 2 different contexts, is a bit
inefficient to call should "say hello" both times or does it really
make that much?

david.l...@gmail.com

unread,
Sep 26, 2009, 3:23:37 PM9/26/09
to sho...@googlegroups.com
I'm not sure what you mean... can you give an example?

-D

Christopher Gardner

unread,
Sep 26, 2009, 3:30:02 PM9/26/09
to sho...@googlegroups.com
"should say hello has been called" is printed twice. What happens
when should "say hello" do ... end is called the second time? I'm
just realizing it is independent of the first call and is simply used
to build the test.

require 'test/unit'
require 'shoulda'

class ShouldaTest < Test::Unit::TestCase

def self.should_say_hello
puts "should say hello has been called"
should "say hello" do
check_hello_world
end
end

context "An new object" do
setup do
@hw = HelloWorld.new
end

should_say_hello
end

context "Another new object" do
setup do
@hw = HelloWorld.new
end

should_say_hello
end

def check_hello_world
assert_equal('hello', @hw.greet)
end
end


Reply all
Reply to author
Forward
0 new messages