[rspec-users] Mocking within a Proc/Block?

David Chelimsky dchelimsky at gmail.com
Thu Oct 25 01:28:07 EDT 2007


On 10/24/07, Evan David Light <evan at tiggerpalace.com> wrote:
> The following works:
>
>    it "should evaluate a passed in block in the context of the
> interview object" do
>      block = Proc.new { raise unless self.is_a?
> (Interview) }.should_not raise_error
>      Interview.create(:title => "Text", &block)
>    end
>
> However, the following does not:
>
>    it "should evaluate a passed in block in the context of the
> interview object" do
>      block = Proc.new { self.should be_an_instance_of(Interview) }
>      Interview.create(:title => "Text", &block)
>    end
>
> I'd far prefer to use the latter.  However "be_an_instance_of" is not
> defined on my Interview class per the interpreter.

That is correct. It gets evaluated, as your example narrative states:
in the context of the interview object.

How about something like this?

it "should evaluate ..." do
    $interview = nil
    Interview.create(:title => "Text") do
      $interview = self
    end
    $interview.should be_an_instance_of(Interview)
  end
end


More information about the rspec-users mailing list