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

  • Home
  • SEARCH
  • 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 9184885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:09:39+00:00 2026-06-17T19:09:39+00:00

I have the following code in my function to retrieve the data from the

  • 0

I have the following code in my function to retrieve the data from the database:

var oldFareSpecification = new ObservationOnOrBeforeDateSpecification(previousOutboundDate));

var oldFare = _fareRepository.Find(oldFareSpecification).SingleOrDefault();

the specification is:

public class ObservationOnOrBeforeDateSpecification : Specification<Fare>
    {
        public ObservationOnOrBeforeDateSpecification(DateTime date)
        : base(fh => EntityFunctions.DiffDays(fh.ObservationTS, date) >= 0)
    {
    }
    }

which uses System.Data.Entity.
Repository is

Repository<TEntity> : IRepository<TEntity>
private readonly DbContext _context;
        private readonly DbSet<TEntity> _entitySet;
public IEnumerable<TEntity> Find(ISpecification<TEntity> criteria)
        {
            return criteria.SatisfyingEntitiesFrom(_entitySet).AsEnumerable();
        }

it works fine, but when I try to Moq repository in test class and make calls to repository returning results of queries on my fake objects, I get an exception:
Function can be called only from LINQ to entities (or very similar – I get it in other language : p).

My test class make database returning fake results this way:

var faresRepository = Fixture.Freeze<Mock<IRepository<DAL.Fare>>>();
faresRepository.Setup(
                fhr => fhr.Find(It.IsAny<ISpecification<Fare>>())).Returns(
                (ISpecification<Fare> spec) => spec.SatisfyingEntitiesFrom(fares.AsQueryable()));

and now my question is how can I code specification that compares the difference in days and works fine called from the code normally and on the Mocked repository. By right compare I mean here, that difference between 2013.01.16 23:55:34 and 2013.01.17 01:55:34 in days returns 1.

  • 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-06-17T19:09:40+00:00Added an answer on June 17, 2026 at 7:09 pm

    Solution:

    ** Only way to do it is to create a queue which returns fake results each time specific method (here .Find) is called. not exactly what I would like to achieve, but at least works. so here it is:

        var faresHistoryRepository = Fixture.Freeze<Mock<IRepository<DAL.FareHistory>>>();
    
        var fareHistoryResults = new Queue<List<FareHistory>>();
    
        var fareHistories = new List<DAL.FareHistory>();
        FareHistory fh1 = new FareHistory { FareHistoryId = fareHistoryId + 1, FareId = fareId, ObservationTS = date.AddDays(-1), StatusId = (int)DAL.Enum.Status.Active };
        fareHistories.Add(fh1);
        fh1 = new FareHistory { FareHistoryId = fareHistoryId + 4, FareId = fareId, ObservationTS = date.AddDays(-4), StatusId = (int)DAL.Enum.Status.Active };
        fareHistories.Add(fh1);
        fh1 = new FareHistory { FareHistoryId = fareHistoryId + 5, FareId = fareId, ObservationTS = date.AddDays(-5), StatusId = (int)DAL.Enum.Status.Active };
        fareHistories.Add(fh1);
        fareHistoryResults.Enqueue(fareHistories); // fare histories
    
        var fareHistoriesSample = new List<DAL.FareHistory>();
        fh1 = new FareHistory { FareHistoryId = fareHistoryId + 6, FareId = oldFareId,
     ObservationTS = depDate.AddDays(-fullDaysDifference), StatusId = (int)DAL.Enum.Status.Active };
        fareHistoriesSample.Add(fh1);
            fh1 = new FareHistory { FareHistoryId = fareHistoryId + 7, FareId = oldFareId,
         ObservationTS = depDate.AddDays(-15), StatusId = (int)DAL.Enum.Status.Active };
            fareHistoriesSample.Add(fh1);
            fh1 = new FareHistory { FareHistoryId = fareHistoryId + 8, FareId = oldFareId,
         ObservationTS = depDate.AddDays(-16), StatusId = (int)DAL.Enum.Status.Active };
                        fareHistoriesSample.Add(fh1);
    
            fareHistoryResults.Enqueue(fareHistoriesSample); // fareHistoriesSample
    
            faresHistoryRepository.Setup(
                                fhr => fhr.Find(It.IsAny<ISpecification<FareHistory>>
        ())).Returns(()=>fareHistoryResults.Dequeue());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made the following code to retrive data from SQLite database. public Cursor
I have written php code to retrieve data from database to div in webpage.i
I have tried to use the following code to retrieve the data from the
I have the following code: function isValidAuthor($authorID){ $query = SELECT * FROM jos_users WHERE
I have the following code: function show(){ var a=document.getElementById('somediv').style.display; a=block; } The above code
I currently have the following js code function clearMulti(option) { var i; var select
I have the following code, which retrieves the page slugs from the database which
I have the following code in the controller: function add() { if (!empty($this->data)) {
I have a php script to retrieve data from a MysQL databse following an
I have the following code: var wait = 500; $(document).on(click,.new_game_list_row,function(event){ var self = $(this);

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.