Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In this sort of situation, I wouldn't verify the calls to @db because those are read-only calls, so I really don't care if they happen or not. Yes, of course they do have to happen (otherwise where is the data coming from), but I don't think of it as an explicit requirement on the behavior of WishlistReporter... If WishlistReporter could produce the report without talking to the database that'd be perfectly fine by me.</p> <p>I would use <code>db.stub!</code> instead of <code>db.should_receive</code> and be perfectly happy with that.</p> <p>But for cases where the calls to the object being mocked have side-effects and <em>are</em> explicit requirements, I do something like this. (In this example, for whatever reason we require that the db object be instructed to reload its data before we query it.) Again, the methods that are returning the data don't need to be explicitly verified, since if the report output is correct, then the data must have been pulled from @db correctly:</p> <pre><code>describe WishlistReporter do before(:each) do @db = double("database") @db.stub!(:reload_data_from_server) @db.stub!(:categories).and_return(["C1"]) @db.stub!(:items).with("C1").and_return(["I1", "I2"]) @db.stub!(:subitems).with("I1").and_return(["S1", "S2"]) @db.stub!(:subitems).with("I2").and_return(["S3", "S4"]) @output = StringIO.new @subject = WishlistReporter.new(@db) end it "should reload data before generating a report" do @db.should_receive(:reload_data_from_server) @subject.report(:text, @output) end it "should output a report" do @subject.report(:text, @output) @output.seek(0) @output.read.should =~ /stuff/ end end </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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