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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:08:19+00:00 2026-05-24T23:08:19+00:00

I am very new to unit testing when it comes to databases and especially

  • 0

I am very new to unit testing when it comes to databases and especially entity framework and I am now stuck. I am using NUnit to test and mock the entities used and am working using a generic repository. My entity framework has a full set of POCO classes and the bit I am currently testing looks like this:

    campaignRepoMock = new DynamicMock(typeof(IRepository<Campaign>));
    campaignRepoMock.ExpectAndReturn("First", testCampaign, new Func<Campaign, bool>(c => c.CampaignID == testCampaign.CampaignID));
    CampaignService campaignService = new CampaignService((IRepository<Campaign>)campaignRepoMock.MockInstance);
    Campaign campaign = campaignService.GetCampaign(testCampaign.Key, ProjectId);
    Assert.AreEqual(testCampaign, campaign);

testCampaign is a single POCO campaign test object. The method “First” in the IRepository looks like the following:

    public T First(Func<T, bool> predicate)
    {
        return _objectSet.FirstOrDefault<T>(predicate);
    }

The error I am getting from Nunit is

CampaignServiceTests.Campaign_Get_Campaign:   
  Expected: <System.Func`2[Campaign,System.Boolean]>  
  But was: <System.Func`2[Campaign,System.Boolean]>

So it is basically saying that it is getting what it is expecting, but its throwing an error? Maybe my understanding of this is all wrong, I just want to test the searching for a Campaign based on its key and the project it is linked to. The GetCampaigns method just search the repository sent to it for a campaign that has both of those items.

Can anyone point me to what I am doing wrong? Thanks in advance.

  • 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-24T23:08:20+00:00Added an answer on May 24, 2026 at 11:08 pm

    If I understand your code, over here

    campaignRepoMock.ExpectAndReturn("First", testCampaign, new Func<Campaign, bool>(c => c.CampaignID == testCampaign.CampaignID));
    

    you are setting up your mock object to return a function that is not identical to your testCampaign.

    Assert.AreEqual() tests for strict equality. testCampaign and campaign are of the same type and have the same content, but refer to different objects.

    What mocking framework are you using? Looks pretty complicated and confusing to me. For starting I would recommend something like Moq

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

Sidebar

Related Questions

I am very new to the entity framework, so please bear with me... How
I am very new to the whole unit testing concept so I'm sorry if
Using Visual Studio 2008 / C# / VS Unit Testing. I have a very
I am very new to unit testing even though i have been coding for
For unit testing you shouldn't test private methods, yes, but for integration tests (using
I'm new to CMake and I'm using the Google Test Framework. I've looked for
I am very, very new to unit testing and am trying to write a
Very new to JQuery and MVC and webdevelopment over all. I'm now trying to
I still very new using Subversion. Is it possible to have a working copy
I'm pretty new to IoC, Dependency Injection and Unit Testing. I'm starting a new

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.