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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:20:57+00:00 2026-05-28T20:20:57+00:00

Here is a very simplistic example of what I’m trying to do: public class

  • 0

Here is a very simplistic example of what I’m trying to do:

public class Bar
{
    public void SomeMethod(string param)
    {
        //whatever
    }
}

public interface IBarRepository
{
    List<Bar> GetBarsFromStore();
}

public class FooService
{
    private readonly IBarRepository _barRepository;

    public FooService(IBarRepository barRepository)
    {
        _barRepository = barRepository;
    }

    public List<Bar> GetBars()
    {
        var bars = _barRepository.GetBarsFromStore();
        foreach (var bar in bars)
        {
            bar.SomeMethod("someValue");
        }
        return bars;
    }
}

In my test, I’m mocking IBarRepository to return a concrete List defined in the unit test and passing that mocked repository instance to the FooService constructor.

I want to verify in the FooService method GetBars that SomeMethod was called for each of the Bars returned from the repository. I’m using Moq. Is there any way to do this without mocking the list of Bars returned (if even possible) and not having to put some hacky flag in Bar (yuck) ?.

I’m following an example from a DDD book, but I’m starting to think it smells because I’m challenged in testing the implementation….

  • 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-28T20:20:58+00:00Added an answer on May 28, 2026 at 8:20 pm

    Revised… this passes:

    public class Bar
    {
        public virtual void SomeMethod(string param)
        {
            //whatever
        }
    }
    
    public interface IBarRepository
    {
        List<Bar> GetBarsFromStore();
    }
    
    public class FooService
    {
        private readonly IBarRepository _barRepository;
    
        public FooService(IBarRepository barRepository)
        {
            _barRepository = barRepository;
        }
    
        public List<Bar> GetBars()
        {
            var bars = _barRepository.GetBarsFromStore();
            foreach (var bar in bars)
            {
                bar.SomeMethod("someValue");
            }
            return bars;
        }
    }
    
    [TestMethod]
    public void Verify_All_Bars_Called()
    {
        var myBarStub = new Mock<Bar>();
        var mySecondBarStub = new Mock<Bar>();
    
        var myBarList = new List<Bar>() { myBarStub.Object, mySecondBarStub.Object };
        var myStub = new Mock<IBarRepository>();
        myStub.Setup(repos => repos.GetBarsFromStore()).Returns(myBarList);
        var myService = new FooService(myStub.Object);
        myService.GetBars();
    
        myBarStub.Verify(bar => bar.SomeMethod(It.IsAny<string>()), Times.Once());
        mySecondBarStub.Verify(bar => bar.SomeMethod(It.IsAny<string>()), Times.Once());
    }
    

    Note the slight change to class Bar (SomeMethod() is virtual). A change, but not one involving a flag… 🙂

    Now, in terms of broader design, there is some mutation going on with your bar (whatever “SomeMethod()” actually does). The best thing to do would probably be to verify that this mutation happened on each Bar returned from FooService.GetBars(). That is, setup your repository stub to return some bars, and then verify that whatever mutation is performed by SomeMethod() has taken place. After all, you control the Bars that will be returned, so you can setup their pre-SomeMethod() state, and then inspect their post-SomeMethod() state.

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

Sidebar

Related Questions

Here is a very strange behavior using EJB beans: @Local public interface Provider {
Here's a very simple Prototype example. All it does is, on window load, an
Here is a very basic class for handling sessions on App Engine: Lightweight implementation
Very confused here, trying out the yuicompressor on a simple javascript file. My js
Note that I'm mirroring the example given here very closely. In fact, my situation
Here is very simplified version of code that i have: class PrintJob : IEntity
I am trying to scrape a table here very similar in structure to my
Here's the very dumb way: def divisorGenerator(n): for i in xrange(1,n/2+1): if n%i ==
Here's a very simple question. I have an SP that inserts a row into
Here is a very and straight forward question: How long will it take to

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.