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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:09:22+00:00 2026-05-28T04:09:22+00:00

The problem: running the code outright creates the proper filtering, and my unit test

  • 0

The problem: running the code outright creates the proper filtering, and my unit test is not filtering at all (returning all records in the mocked repository).

I can’t tell if having the expression logic in the test is screwing something up or what, but no matter the criteria I set, the mocked repository is not filtered on ad I can back “all” records. This works 100% from the service layer calling it, but not in tests.

Edit: sorry for the formatting of the code, I couldn’t get it any better.

Code:

public abstract class EFRepository<T> : IRepository<T> where T : BaseEFModel {

public IUnitOfWork UnitOfWork { get; set; }

private IDbSet<T> _objectset;
private IDbSet<T> ObjectSet
{
    get { return _objectset ?? (_objectset = UnitOfWork.Context.Set<T>()); }
}

public virtual IQueryable<T> WhereInternal(Expression<Func<T, bool>> expression)
{
    return ObjectSet.Where(expression);
} } 

implementation:

public class DoNotSolicitRepo : EFRepository<DoNotSolicit>, IDoNotSolicitRepo {
private readonly RestUnitOfWork worker;

public DoNotSolicitRepo(RestUnitOfWork _worker)
{
    worker = _worker;
}

public IList<DNSContract> SelectWithCriteria(DNS_Search search)
{
    // create the where clause 
    Expression<Func<DoNotSolicit, bool>> whereClause = c => (
        (String.IsNullOrEmpty(search.FirstName) || c.FirstName.StartsWith(search.FirstName)) &&
        (String.IsNullOrEmpty(search.LastName) || c.LastName.StartsWith(search.LastName)) &&
        (String.IsNullOrEmpty(search.Address1) || c.Address1.Contains(search.Address1)) &&
        (String.IsNullOrEmpty(search.Address2) || c.Address2.Contains(search.Address2)) &&
        (String.IsNullOrEmpty(search.City) || c.City.Contains(search.City)) &&
        (String.IsNullOrEmpty(search.State) || c.State.Equals(search.State)) &&
        (String.IsNullOrEmpty(search.Zip5) || c.Zip.Equals(search.Zip5)) &&
        (String.IsNullOrEmpty(search.Phone) || c.Phone.Equals(search.Phone)) &&
        (String.IsNullOrEmpty(search.Email) || c.Email.Equals(search.Email))
        );

    using (var scope = worker)
    {
        scope.Register(this);
        var resultList = WhereInternal(whereClause).ToList();

        Mapper.CreateMap<DoNotSolicit, DNSContract>()
           .ForMember(dest => dest.PartnerCode, opt => opt.Ignore())
           .ForMember(dest => dest.PartnerDescription, opt => opt.Ignore())
           .ForMember(dest => dest.DoNotSolicitReason, opt => opt.Ignore())
           .ForMember(dest => dest.SaveDate, opt => opt.Ignore())
           .ForMember(dest => dest.InsertDT, opt => opt.Ignore());

         var returnObj = Mapper.Map<IList<DoNotSolicit>, IList<DNSContract>>(resultList);

         return returnObj.FriendlySaveDates();
    }
} }

Test:

base:

public abstract class BaseEFUnitFixture<T> where T : BaseEFModel {
protected Mock<EFRepository<T>> mockedEFRepo = new Mock<EFRepository<T>>();

public Mock<EFRepository<T>> MockedEFRepositiory()
{
    var t = new List<T>();

    mockedEFRepo.Setup(x => x.AddInternal(It.IsAny<T>())).Callback((T e) => t.Add(e));
    mockedEFRepo.Setup(x => x.AddInternal(It.IsAny<List<T>>())).Callback((IList<T> le) => t.AddRange(le));
    mockedEFRepo.Setup(x => x.AllInternal()).Returns(t.AsQueryable());
    mockedEFRepo.Setup(x => x.WhereInternal(It.Is<Expression<Func<T, bool>>>(y => y != null))).Returns(t.AsQueryable());

    return mockedEFRepo;
}

}

implementation:

[TestFixture] public class DNSRepoTest : BaseEFUnitFixture<DoNotSolicit> {
private readonly List<DoNotSolicit> list = new List<DoNotSolicit>();

private class Search
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip5 { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
}

private Expression<Func<DoNotSolicit, bool>> SearchBuilder(Search search)
{
    //  same as repo logic
    Expression<Func<DoNotSolicit, bool>> whereClause = c => (
       (String.IsNullOrEmpty(search.FirstName) || c.FirstName.StartsWith(search.FirstName)) &&
       (String.IsNullOrEmpty(search.LastName) || c.LastName.StartsWith(search.LastName)) &&
       (String.IsNullOrEmpty(search.Address1) || c.Address1.Contains(search.Address1)) &&
       (String.IsNullOrEmpty(search.Address2) || c.Address2.Contains(search.Address2)) &&
       (String.IsNullOrEmpty(search.City) || c.City.Contains(search.City)) &&
       (String.IsNullOrEmpty(search.State) || c.State.Equals(search.State)) &&
       (String.IsNullOrEmpty(search.Zip5) || c.Zip.Equals(search.Zip5)) &&
       (String.IsNullOrEmpty(search.Phone) || c.Phone.Equals(search.Phone)) &&
       (String.IsNullOrEmpty(search.Email) || c.Email.Equals(search.Email))
       );

    return whereClause;
}

[TestFixtureSetUp]
public void Init()
{
    list.Add(new DoNotSolicit
                 {
                     DoNotSolicitID = 4,
                     FirstName = "nunit",
                     Origination = "testing"
                 });

    mockedEFRepo = MockedEFRepositiory();
    mockedEFRepo.Object.AddInternal(list);
}

[Test]
public void SelectWithCriteria_FirstNameMatch()
{
    var clause = SearchBuilder(new Search{FirstName = "test"});
    var results = mockedEFRepo.Object.WhereInternal(clause).ToList();

    Assert.IsNotNull(results);
    Assert.IsTrue(results.Count < mockedEFRepo.Object.AllInternal().Count());
    Assert.IsTrue(results.Count > 0);
} }
  • 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-28T04:09:23+00:00Added an answer on May 28, 2026 at 4:09 am

    You are wrong with your approach in general. What you do you mock the class that you test – it is incorrect. You should only mock something external to the class you are testing – because what mocking does is pretty much replacing the functionality with of mocked object with empty stubs. If it’s mocked it doesn’t work or work as the mock is configured.

    I see no reason why you would want to test the mocked class, as in this case what you are testing is not the class functionality/code but the way you configured your mock.

    It’s quite hard to understand what code do you want your test method to test. I would suggest doing the dependency injection to separate the actual data repository (which could be mocked with specific data/methods) and the class that contains logic (like selecting the first match). Pass mocked repository in the constructor of your logic class and test it asserting the expected behavior.

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

Sidebar

Related Questions

I had no problem at all running the following code on a local server,
I'm a beginner with jdbc ... I have a problem running this code :
I encountered a problem when running some old code that was handed down to
Trying to truncate some code here and running into a problem: <script type=text/javascript> $(function()
I'm working on writing some automation code and I'm running into a problem finding
I'm having a problem running a VS 2005 app on some machines and not
I'm having a problem when running my PHPUnit test of creating a new profile,
Currently learning Scheme/Racket and have problem running this piece of code. (if (or (<
I am running code review tools on a large application. The code is all
I have a problem in running the cron job. here are my code: <?php

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.