Note that there are some explanatory texts on larger screens.

plurals
  1. PORSpec & DataMapper: How to stub the finder method correctly
    primarykey
    data
    text
    <p>The environment is Sinatra, Nokogiri, RSpec, FactoryGirl and DataMapper. </p> <p>The problem is with DataMapper's serializer. I don't know how to stub appropriatetely with it.</p> <p>The problematic code:</p> <pre><code>specify 'should return an xml array of all municipalities' do municipalities = [] 10.times { |n| municipalities &lt;&lt; Factory.build(:municipality, :code =&gt; "Municipality no #{n}") } Municipality.stub!(:all).and_return(municipalities) get "/municipalities.xml" # ^------- KABOOM! # [.. rest of code clipped out ..] end </code></pre> <p>The code which <code>get</code> calls is practically equivalent with:</p> <pre><code> Municipality.all.to_xml </code></pre> <p>The problem is that the result set's type is <code>DataMapper::Collection</code> which has the <code>#to_xml</code> method. </p> <p>My stub does not obviously work because I use an <code>Array</code> of stubbed instances, instead of a <code>DataMapper::Collection</code>.</p> <p>My question is: </p> <p>How should I create a collection of instances (with FactoryGirl) which are of type <code>DataMapper::Collection</code> and have the <code>#to_xml</code> method provided by DataMapper's serializer?</p> <p>The code is also available at Github:</p> <ul> <li><a href="https://github.com/pre/apila/blob/69d6e02c3fc8019dc8cc35c5b568188140a92277/spec/application_spec.rb#L67" rel="nofollow">Spec</a></li> <li><a href="https://github.com/pre/apila/blob/69d6e02c3fc8019dc8cc35c5b568188140a92277/application.rb#L55" rel="nofollow">Controller</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload