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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:27:38+00:00 2026-05-24T22:27:38+00:00

I am trying to mock my repository’s Get() method to return an object in

  • 0

I am trying to mock my repository’s Get() method to return an object in order to fake an update on that object, but my setup is not working:

Here is my Test:

[Test]
public void TestUploadDealSummaryReportUploadedExistingUpdatesSuccessfully()
{
    var dealSummary = new DealSummary {FileName = "Test"};
    _mockRepository.Setup(r => r.Get(x => x.FileName == dealSummary.FileName))
        .Returns(new DealSummary {FileName = "Test"}); //not working for some reason...

    var reportUploader = new ReportUploader(_mockUnitOfWork.Object, _mockRepository.Object);
    reportUploader.UploadDealSummaryReport(dealSummary, "", "");

    _mockRepository.Verify(r => r.Update(dealSummary));
    _mockUnitOfWork.Verify(uow => uow.Save());
}

Here is the method that is being tested:

public void UploadDealSummaryReport(DealSummary dealSummary, string uploadedBy, string comments)
{
    dealSummary.UploadedBy = uploadedBy;
    dealSummary.Comments = comments;

    // method should be mocked to return a deal summary but returns null
    var existingDealSummary = _repository.Get(x => x.FileName == dealSummary.FileName);
    if (existingDealSummary == null)
        _repository.Insert(dealSummary);
    else
        _repository.Update(dealSummary);

    _unitOfWork.Save();
}

And here is the error that I get when I run my unit test:

Moq.MockException :
Expected invocation on the mock at least once, but was never performed: r => r.Update(.dealSummary)
No setups configured.

Performed invocations:
IRepository1.Get(x => (x.FileName == value(FRSDashboard.Lib.Concrete.ReportUploader+<>c__DisplayClass0).dealSummary.FileName))
IRepository
1.Insert(FRSDashboard.Data.Entities.DealSummary)
at Moq.Mock.ThrowVerifyException(MethodCall expected, IEnumerable1 setups, IEnumerable1 actualCalls, Expression expression, Times times, Int32 callCount)
at Moq.Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times)
at Moq.Mock.Verify(Mock mock, Expression1 expression, Times times, String failMessage)
at Moq.Mock
1.Verify(Expression`1 expression)
at FRSDashboard.Test.FRSDashboard.Lib.ReportUploaderTest.TestUploadDealSummaryReportUploadedExistingUpdatesSuccessfully

Through debugging I have found that the x => x.FileName is returning null, but even if i compare it to null I still get a null instead of the Deal Summary I want returned. Any ideas?

  • 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-24T22:27:39+00:00Added an answer on May 24, 2026 at 10:27 pm

    I’m guessing your setup isn’t matching the call you make because they’re two different anonymous lambdas. You may needs something like

    _mockRepository.Setup(r => r.Get(It.IsAny<**whatever your get lambda is defined as**>()).Returns(new DealSummary {FileName = "Test"});
    

    You could verify by setting a breakpoint in the Get() method of your repository and seeing if it is hit. It shouldn’t be.

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

Sidebar

Related Questions

I'm trying to mock a data repository object but after setting an expectation on
I am trying to Mock an object that is being passed into another object,
I've been trying to get to mock a method with vararg parameters using Mockito:
Im trying to mock a method that is ineherited from a parent class that
I'm trying to unit test a method that performs a fairly complex operation, but
I have Equals method that I am trying to tests. this method return true
I am trying the following, but I am getting : Object reference not set
I'm trying to mock the RabbitMQ ConnectionFactory object to return a mocked connection, using
I am trying to mock out a private method that is making a JNDI
I'm trying to use mock to verify that an index property has been set.

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.