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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:13:47+00:00 2026-05-30T18:13:47+00:00

Iam writing unit tests for my repository and I need to test repository method

  • 0

Iam writing unit tests for my repository and I need to test repository method with delegate.

First of all part from repository interface

public interface IRepository<T>
{
    ...
    IQueryable<T> All();
    ...
}

and here is the method what I want to test. I do it as an extension method for IRepository<RentDetails>

public static bool AnyIntersectionForRoomWithDatesExists
(this IRepository<RentDetails> repository, Room room, DateTime startDate, 
DateTime terminationDate)
{
var rent = repository.All().Where(
r =>
(r.BeginningDate <= startDate && r.TerminationDate > terminationDate &&
          r.TerminationDate < terminationDate) ||
(r.BeginningDate > startDate) ||
(r.TerminationDate >= terminationDate && r.BeginningDate > startDate &&
          r.BeginningDate < terminationDate));
        }

In my unit tests I created an IEnumerable<RentDetails> collection

IEnumerable<RentDetails> rentDetailsColection = new List<RentDetails>
{
new RentDetails(TenantTestHelper.ValidTenant,roomMoq, new DateTime(2012, 1, 1),new DateTime(2012, 2, 1)),
new RentDetails(TenantTestHelper.ValidTenant,roomMoq, new DateTime(2012, 3, 1),new DateTime(2012, 7, 1)),
new RentDetails(TenantTestHelper.ValidTenant,roomMoq, new DateTime(2012, 10, 1),DateTime.MaxValue)
};

but I have a problem how to setup the repository moq? I tryed the following

[Test]
var reqestStartDate = new DateTime(2012, 4, 1);
var requestTerminationDate = reqestStartDate.AddDays(30);

...

rentDetailsRepositoryMoq.Setup(c => c.All().Where(r =>
(r.BeginningDate <= reqestStartDate &&
r.TerminationDate > requestTerminationDate &&
r.TerminationDate < requestTerminationDate) ||
(r.BeginningDate > reqestStartDate) ||
(r.TerminationDate >= requestTerminationDate &&
r.BeginningDate > reqestStartDate &&
r.BeginningDate < requestTerminationDate)))
.Returns((IQueryable<RentDetails>)rentDetailsColection
.Where(r =>(r.BeginningDate <= reqestStartDate &&
r.TerminationDate > requestTerminationDate &&
r.TerminationDate < requestTerminationDate) ||
(r.BeginningDate > reqestStartDate) ||
(r.TerminationDate >= requestTerminationDate &&
r.BeginningDate > reqestStartDate && r.BeginningDate < requestTerminationDate)));

but I have got an exception

NotSupportedException

Expression references a method that does not belong to the mocked object: c => c.All().Where(r => (((r.BeginningDate <= .reqestStartDate && r.TerminationDate > .requestTerminationDate) && r.TerminationDate < .requestTerminationDate) || r.BeginningDate > .reqestStartDate) || ((r.TerminationDate >= .requestTerminationDate && r.BeginningDate > .reqestStartDate) && r.BeginningDate < .requestTerminationDate))

  • 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-30T18:13:48+00:00Added an answer on May 30, 2026 at 6:13 pm

    Just Setup the IRepository.All() to return a IQueryable<T> and assert that the unit under test is doing what it is supposed to be doing. Note that by conventional definitions, you are trying to Stub the repository and you will be asserting the state.

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

Sidebar

Related Questions

I am writing my first unit tests with Test::Unit and I have reached a
I am writing some unit tests for an extension method I have written on
Disclaimer: This is my first time writing unit tests...be gentle! :) I am trying
I am writing unit tests for a multi-threading application, where I need to wait
I am writing some unit tests in which I need a fake xml file.
I am writing unit tests in visual studio 2010. For test some functionality, I
I am writing unit tests in GHUnit test framework. I have a NSMutableDictionary detailsDict
I am writing (junit) unit tests for a class which implements an exposed interface
I am writing a set of integration tests (Unit tests with MS Test which
I am writing unit tests to test an MVC application. How can I validate

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.