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 6756995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:35:36+00:00 2026-05-26T13:35:36+00:00

I’m trying to mock (using Rhino.Mocks) a DataService which returns an IDataReader. I Can

  • 0

I’m trying to mock (using Rhino.Mocks) a DataService which returns an IDataReader. I Can mock the IDataReader (StackOverFlow Q 1792984) but if I then Stub out A Data Service which returns this DataReader and inject it into the method I am trying to test it the stub returns only a null Data Reader.

IDataService

public interface IDataService
{
 SqlDataReader ExecuteReader(string cmdText, CommandType commandType);
 <rest of definition ommited
}

Unit test

    [TestMethod]
    public void GetCustomer_Pass()
    {
        //Arrange
        var customer = new Customer()
                       {
                         Id = 1, FirstName = "Patrick",
                         LastName = "Hastings", ProfilePictureURL = ""
                       };

        var mockDataService = MockRepository.GenerateStub<IDataService>();

        var reader = MockRepository.GenerateStub<IDataReader>();
        reader.Stub(x => x.Read()).Repeat.Once().Return(true);
        reader.Stub(x => x.Read()).Return(false);
        reader.Stub(x => x["Id"]).Return(customer.Id);
        reader.Stub(x => x["FirstName"]).Return(customer.FirstName);
        reader.Stub(x => x["LastName"]).Return(customer.LastName);
        reader.Stub(x => x["ProfilePictureURL"]).Return(customer.ProfilePictureURL);

        mockDataService.Stub( s =>
            s.ExecuteReader(string.Format("select FirstName, LastName,ProfilePictureURL from customer where Id = {0}", customer.Id),
                            CommandType.Text))
                            .Return(reader as SqlDataReader);

        var custRepository = new CustomerRepository(mockDataService);

        //Act
        var customer2 = custRepository.GetCustomer(1);
        //Assert
        Assert.AreEqual(customer.FirstName, customer2.FirstName,"FirstName Mismatch");
        Assert.AreEqual(customer.LastName, customer2.LastName, "LastName Mismatch");
    }

Method UnderTest

    public CustomerRepository(IDataService dataService) //Constructor
    {
        if (dataService == null) throw new ArgumentNullException("dataService", "dataService cannot be null.");
        this.dataService = dataService;
    }

    public Customer GetCustomer(int id)
    {
        var sql =string.Format("select FirstName, LastName, ProfilePictureURL from customer where Id = {0}",id);
        var dr = dataService.ExecuteReader(sql, CommandType.Text);
        Customer customer ;
        using (dr)
        {
            if (!dr.Read()){return null;}
            customer = new Customer
                         {
                             FirstName = dr["FirstName"].ToString(),
                             LastName = dr["LastName"].ToString(),
                             ProfilePictureURL = dr["ProfilePictureURL"].ToString()
                         };
        }
        return customer;
    }

This issue that I get is that when “dataService.ExecuteReader(sql, CommandType.Text);” should return the mocked IDataReader but instead returns Null – any pointers as to what I am doing wrong would be gratefully appreciated.

  • 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-26T13:35:37+00:00Added an answer on May 26, 2026 at 1:35 pm

    First point:

    I believe because of different arguments passed into the ExecuteReader() method:

    Set in expectation:

    "select FirstName, LastName,ProfilePictureURL...
    

    Passed whilst actual call: (extra space between comma and ProfilePictureURL)

    "select FirstName, LastName, ProfilePictureURL...
    

    Sometimes IgnoreArguments() is pretty useful when argument itself does not matter

    Second Point:

    .Return(reader as SqlDataReader);
    

    This is invalid cast because reader is a mock object created by RhinoMock.

    I believe you do not need a SqlDataReader type so just return reader itself

     .Return(reader)
    

    PS: Also I would suggest store values which are repeating as constants, you can store customerId = 1 as constant as well to avoid such possible issues.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

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.