[rspec-users] Testing methods that yields a block

ignacy.moryc imoryc at gmail.com
Thu Oct 7 10:24:19 EDT 2010


I want to test a class method that sets some state and then yields a
block. It looks something like that:

class Cat
  class << self
     def start
           id = get_uuid
           begin
              yield if block_given?
           ensure
               set_some_other_state
           end
     end
   end
   #...
end

How can I test that id field was set? Or that set_some_other_state
method was called, if I intend to use this class like this:

Cat.start do
   # do what cat's do
end

I'm trying to do this like this:

Cat.start { #some block }
Cat.id.should be_something

but this doesn't work.

I also used:

Cat.should_receive(:id=).once
Cat.start {#some block}

but this doesn't let me see to what the id field was set.

Is there a better way to test this method?

Thanks,




More information about the rspec-users mailing list