# How to spec within a block?

**URL:** <https://www.ruby-forum.com/t/how-to-spec-within-a-block/160000>\
**Category:** RSpec\
**Created:** [February 10, 2009, 9:02pm UTC](https://www.ruby-forum.com/t/how-to-spec-within-a-block/160000 "2009-02-10T21:02:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [February 10, 2009, 9:02pm UTC](https://www.ruby-forum.com/t/how-to-spec-within-a-block/160000/1 "2009-02-10T21:02:32Z")

</div>

I have this and I want to mock the to\_xml but not his block cause this  
is what I want to spec. Is there a way to do this?

Sorry for the formatting

```
  output = @detenteur.to_xml( :skip_types => false, :skip_instruct

```

=\> true, :dasherize =\> false, :only =\> [:inte\_no] ) do |xml\_detenteur|

```
    p xml_detenteur.intervenant_adresses.class.to_s

    xml_detenteur.intervenant_adresses do

      get_object_xml( @detenteur.intervenant_adresses,

```

@detenteur.intervenant\_adresses.obtenir\_adresses\_vigueur, {:builder =\>  
xml\_detenteur, :skip\_instruct =\> true, :skip\_types =\>  
false, :dasherize =\> false, :except =\> [:inte\_no]})  
end

```
    xml_detenteur.intervenant_telephones do
      get_object_xml( @detenteur.intervenant_telephones,

```

@detenteur.intervenant\_telephones.obtenir\_tels\_vigueur, {:builder =\>  
xml\_detenteur, :skip\_instruct =\> true, :skip\_types =\>  
false, :dasherize =\> false, :except =\> [:inte\_no]})  
end

```
    xml_detenteur.intervenants_polices do
      @polices = polices(@detenteur, @police_no, inte_polices,

```

xml\_detenteur)  
end  
end

---

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [February 12, 2009, 2:25am UTC](https://www.ruby-forum.com/t/how-to-spec-within-a-block/160000/2 "2009-02-12T02:25:19Z")

</div>

On Sat, Feb 7, 2009 at 3:47 AM, rgagnon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have this and I want to mock the to\_xml but not his block cause this  
> is what I want to spec. Is there a way to do this?

Use and\_yield to yield an object, and you can set expectations on it.  
For example:

xml\_detent = mock(‘xml detenteur’)  
xml\_detent.stub!(:intervenant\_adresses).and\_return(array\_of\_addresses)  
detenteur.stub!(:to\_xml).and\_yield(xml\_detent)

Does that make sense?

Pat

---

<div class="post-metadata">

**Author:** ![rgagnon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@rgagnon](https://www.ruby-forum.com/u/rgagnon)\
**Post date:** [February 12, 2009, 4:14am UTC](https://www.ruby-forum.com/t/how-to-spec-within-a-block/160000/3 "2009-02-12T04:14:51Z")

</div>

It works thanks for the hints

RÃ©mi
