Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6172355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:22:41+00:00 2026-05-23T23:22:41+00:00

Partly following on from this question. Hopefully the example speaks for itself: there’s a

  • 0

Partly following on from this question. Hopefully the example speaks for itself: there’s a WishlistReporter class which asks one object for data and outputs to another object.

The problem is that with the double for DB, I’m actually testing a whole bunch of things in a single example. Which is not ideal.

I can split the report() method into gather_data() and output() methods. But that doesn’t help: in order to test the output() method I would still need to create the mock db and run gather_data() again.

Is there a way around this?

describe WishlistReporter do

  it "should talk to the DB and output a report" do
    db = double("database")
    db.should_receive(:categories).and_return(["C1"])
    db.should_receive(:items).with("C1").and_return(["I1", "I2"])
    db.should_receive(:subitems).with("I1").and_return(["S1", "S2"])
    db.should_receive(:subitems).with("I2").and_return(["S3", "S4"])

    wr = StringIO.new

    r = WishlistReporter.new(db)
    r.report(db, :text, wr)

    wr.seek(0)
    wr.read.should =~ /stuff/
  end
end

(In reference to the previous question: I’m perfectly happy to mock the Db class because I consider its interface to be external: part of the “what” not the “how”.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:22:42+00:00Added an answer on May 23, 2026 at 11:22 pm

    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.

    I would use db.stub! instead of db.should_receive and be perfectly happy with that.

    But for cases where the calls to the object being mocked have side-effects and are 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:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am following up from this question here The problem I have is that
i have the following code, taken partly from a Red Black Tree Java implementation.
I'm getting the following from a third party library (one example): @%SystemRoot%\SomePath\SomeFile.Dll,-203 I know
I have this question about c# language's dynamic binding behavior. Consider the following object
Problem: I have to design an algorithm, which does the following for me: Say
This is going to be a pretty long question, so bear with me. I'm
I'm running a third party script by using a wrapper class I've written which
I'm interesting in creation of PushCertWebRequest (this is from apple documentation) I have found
Let me begin by stating that this is a question of aesthetics. I've solved
First, this is a very basic question that I'm unsure of how to phrase.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.