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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:05:51+00:00 2026-05-25T10:05:51+00:00

In the test below, the mocked datareader returns the desired value the first time,

  • 0

In the test below, the mocked datareader returns the desired value the first time, but then returns the same value when the index should be 1.

Am I misusing the dataReader or Rhino stub syntax? What is the fix?

Cheers,
Berryl

failing test

[Test]
public void NullSafeGet_GetsBothProperties()
{
    var sessionImplementor = MockRepository.GenerateStub<ISessionImplementor>();
    var userType = new DateRangeUserType();

    var reader = MockRepository.GenerateStub<IDataReader>();           
    var start = new DateTime(2011, 6, 1);
    var end = new DateTime(2011, 7, 1);
    reader.Stub(x => x[0]).Return(start);
    reader.Stub(x => x[1]).Return(end);    ***<==== returns Jun 1 instead of Jul1

    var result = userType.NullSafeGet(reader, userType.PropertyNames, sessionImplementor, null);
    Assert.That(result, Is.EqualTo(new DateRange(start, end, DateRange.MaxSupportedPrecision)));

}
Expected: <6/1/2011 12:00 AM - 7/1/2011 12:00 AM>
But was:  <6/1/2011 12:00 AM - 6/1/2011 12:00 AM>

SUT (NHib CompositeUserType method)

public override object NullSafeGet(IDataReader dr, string[] names, ISessionImplementor session, object owner) {
    if (dr == null) return null;

    var foundStart = (DateTime)NHibernateUtil.DateTime.NullSafeGet(dr, names[0], session, owner);
    var foundEnd = (DateTime)NHibernateUtil.DateTime.NullSafeGet(dr, names[1], session, owner);

    var precision = DateRange.MaxSupportedPrecision;
    var startDp = _getDatePoint(foundStart, precision);
    var endDp = _getDatePoint(foundEnd, precision);

    return new DateRange(startDp, endDp, precision);
}
  • 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-25T10:05:51+00:00Added an answer on May 25, 2026 at 10:05 am

    You are not mocking everything that is called by NHibernate. This is roughly what NHibernate does with a reader:

    ...
    int index = reader.GetOrdinal(name);
    ...
    if (reader.IsDBNull(index)) {
        return null;
    } else {
        ...
        val = rs[index];
        ...
    }
    

    Stub generated by Rhino will return 0 in response both GetOrdinal calls and it this is why it will return June1 both times. You can try to fix it by mocking GetOrdinal as well as indexer. Like this:

    var reader = MockRepository.GenerateStub<IDataReader>();
    var start = new DateTime(2011, 6, 1);
    var end = new DateTime(2011, 7, 1);
    
    reader.Stub(x => x.GetOrdinal(userType.PropertyNames[0])).Return(0);
    reader.Stub(x => x.GetOrdinal(userType.PropertyNames[1])).Return(1);
    
    reader.Stub(x => x[0]).Return(start);
    reader.Stub(x => x[1]).Return(end);
    

    But it might be worth reconsidering whether you really need to unit test UserType. It does not have a lot of responsibility other than calling NHibernate. Unit testing this class requires you to mock type you don’t own (MS IDataReader). What’s even worse is that this mock is used by another thirdparty (NHibernate). Essentially you need to look at NHibernate source code (which is what I did) to create a correct stub. Take a look at this article. It goes into a lot more details about why you should avoid mocking types that you don’t own. You may be better off writing integration test for this class, using in-memory sqlite database.

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

Sidebar

Related Questions

In the below code onclick edit how can the value of tag test be
I'm doing some file io and created the test below, but I thought testoutput2.txt
Code for the below test: http://jsfiddle.net/fXdjm/ Questions about line heights: Please refer first box.
In the test below, the Bar and Baz blocks contain identical specs. Leaving aside
I am doing the below test to try and learn more about LINQ to
The code looks like below: namespace Test { public interface IMyClass { List<IMyClass> GetList();
the code below gives compilation error when I try to create test t[2]; because
The script below, test.php, is intended to be placed in a specific directory of
I have the code below in my test code in many places: // //
When I run a Test Project on Visual Studio I use the code below

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.