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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:04:29+00:00 2026-06-03T21:04:29+00:00

This is my test [TestClass] public class RepositoryTests { private APurchaseOrderRepository _repository; [TestInitialize] public

  • 0

This is my test

[TestClass]
public class RepositoryTests
{
    private APurchaseOrderRepository _repository;

    [TestInitialize]
    public void TestInitialize()
    {
        _repository = new FakePurchaseOrderRepository();
    }

    [TestMethod]
    public void RepositoryGetPurchaseOrdersForStoreCallsValidatePurchaseOrders()
    {
        var store = new Store();
        var mockRepo = new Mock<APurchaseOrderRepository>();
        mockRepo.Protected().Setup("ValidatePurchaseOrders", ItExpr.IsAny<List<PurchaseOrder>>());

        _repository.GetPurchaseOrders(store);

        mockRepo.Protected().Verify("ValidatePurchaseOrders", Times.Once(), ItExpr.IsAny<List<PurchaseOrder>>());
    } 
}

APurchaseOrderRepository and it’s interface look like this

public interface IPurchaseOrderRepository
{
    List<PurchaseOrder> GetPurchaseOrders(Store store);
}

public abstract class APurchaseOrderRepository : IPurchaseOrderRepository
{
    public abstract List<PurchaseOrder> GetPurchaseOrders(Store store);

    protected virtual bool ValidatePurchaseOrders(List<PurchaseOrder> purchaseOrders)
    {
        return true;
    }
}

And my Fake

public class FakePurchaseOrderRepository : APurchaseOrderRepository
{
    public override List<PurchaseOrder> GetPurchaseOrders(Store store)
    {
        var purchaseOrders = new List<PurchaseOrder>();

        ValidatePurchaseOrders(purchaseOrders);

        return purchaseOrders;
    }
}

However, my test fails with:

Test method
PreSwapTests.RepositoryTests.RepositoryGetPurchaseOrdersForStoreCallsValidatePurchaseOrders
threw exception: Moq.MockException: Expected invocation on the mock
once, but was 0 times: mock =>
mock.ValidatePurchaseOrders(It.IsAny())

Configured setups: mock =>
mock.ValidatePurchaseOrders(It.IsAny()), Times.Never No
invocations performed.

What am I doing wrong?

Notes:

  • Moq.4.0.10827

Update:

I think it is this line mockRepo.Protected().Setup("ValidatePurchaseOrders");, because I need to add the parameters to it as a second argument, but I can’t seem to get it correct.

Update 2:
Made some modifications, now it compiles, but isn’t counting correctly…or something, error message and code are both updated above.

  • 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-03T21:04:31+00:00Added an answer on June 3, 2026 at 9:04 pm

    Realized I was doing this all wrong, changed my objects to work with this test

        [TestMethod]
        public void RepositoryGetPurchaseOrdersForStoreCallsValidatePurchaseOrders()
        {
            var store = new Store();
    
            var mockPurchaseOrderProvider = new Mock<IPurchaseOrderProvider>();
            var mockPurchaseOrderValidator = new Mock<IPurchaseOrderValidator>();
            var purchaseOrderRepository = new PurchaseOrderRepository(mockPurchaseOrderProvider.Object, mockPurchaseOrderValidator.Object);
    
            mockPurchaseOrderValidator.Setup(x => x.ValidatePurchaseOrders(It.IsAny<List<PurchaseOrder>>()));
    
            purchaseOrderRepository.GetPurchaseOrders(store);
    
            mockPurchaseOrderValidator.Verify(x => x.ValidatePurchaseOrders(It.IsAny<List<PurchaseOrder>>()), Times.Once());
        }
    

    This is a much better structure now I think.

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

Sidebar

Related Questions

Given this test fixture: [TestClass] public class MSTestThreads { [TestMethod] public void Test1() {
consider the following test class: [TestClass] public class ExampleTests { [TestMethod] public void FileDoesNotExists()
Check out this test: [TestFixture] public class Quick_test { [Test] public void Test() {
Pretty simple test: [TestClass] public class MyTestClass { private TestContext _testContext; protected TestContext TestContext
Shouldn't this test pass: [TestMethod] public void derived_message_subscription() { bool handled = false; Messenger.Default.Register<GenericMessage<baseClass>>(this,
Why does this test program result in a java.lang.IllegalMonitorStateException ? public class test {
So, I was not totally sure this was true: [TestClass] public class UnitTest1 {
This is my service class: public class MyService { private readonly MyDataContext _db; public
Consider the following class: public class MyIntSet { private List<int> _list = new List<int>();
I have this first integration test for a tic tac toe game: [TestClass] public

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.