[rspec-users] Testing methods that yields a block

David Chelimsky dchelimsky at gmail.com
Tue Oct 12 02:03:14 EDT 2010


On Thu, Oct 7, 2010 at 9:24 AM, ignacy.moryc <imoryc at gmail.com> wrote:
> 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.

What do you mean "doesn't work"? Are you getting a failure message?
What does it say?

>
> 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,
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list