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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:49:09+00:00 2026-05-27T13:49:09+00:00

I’m working on a sync engine. I have a main engine class that gets

  • 0

I’m working on a sync engine. I have a main engine class that gets two lists of PickLists from two providers (objects passed in as constructors). The method SyncPickLists() calls methods on the underlying objects (the two providers + a logger), an internal method to get a list of everything to do, and then does it as such:

  public class SyncEngine {
    public virtual ILoggingProvider Logger { get; private set; }
    public virtual ICrmProvider CrmProvider { get; private set; }
    public virtual ICacheProvider CacheProvider { get; private set; }

    public SyncEngine(ILoggingProvider loggingProvider, ICrmProvider crmProvider, ICacheProvider cacheProvider) {
      Logger = loggingProvider;
      CrmProvider = crmProvider;
      CacheProvider = cacheProvider;
    }

    public virtual void SyncPickLists() {
      Logger.LogBeginPicklistSync();

      // get all the pick lists from the local cache
      var localCachePickLists = CacheProvider.GetPickLists().ToList();
      // get all the pick lists from the remote system
      var crmPickLists = CrmProvider.GetPickLists().ToList();

      // build a sync plan
      var changes = BuildPickListUpdatePlan(localCachePickLists, crmPickLists).ToList();

      // run the sync
      RunPickListSync(changes);

      Logger.LogEndPicklistSync();
    }

    private IEnumerable<PickListAction> BuildPickListUpdatePlan(List<PickList> localCachePickLists, List<PickList> crmPickLists) {
      ...
    }
  }

I’m trying to use Moq to mock and test this starting with the sync engine, but running into issues checking ot see methods on the logger are called and evaluating the results that are generated from the BuildPickListUpdatePlan() method. I think this is because I’m trying to prime the providers and just call SyncPickLists()… which could be the wrong way to do this. Maybe I should make the BuildPickListUpdatePlan() method public/internal (and decoate the assembly appropriately) and just test that? Looking for input here as I’m new to mocking and not sure i”m going about this the right way.

Here’s what my test looks like so far, but it isn’t complete, correct or doing what I need it to do…

[TestMethod]
public void TestPickListSync() {
  // assign
  var _fakeCrmProvider = new Mock<ICrmProvider>().Object;
  var _fakeCacheProvider = new Mock<ICacheProvider>().Object;
  var _fakeLoggingProvider = new Mock<ILoggingProvider>().Object;
  var _fakeSyncEngine = new Mock<CustomerSyncEngine>(_fakeLoggingProvider, _fakeCrmProvider, _fakeCacheProvider).Object;

  // set picklists for CRM provider
  var crmList1 = new List<string>() { "AAA", "CCC", "DDD" };
  var crmList2 = new List<string>() { "WWW", "XXX", "YYY" };
  var crmPickLists = new List<PickList>() {
    new PickList(){ InternalName = "PickList1", DisplayName = "PickList1", Values = crmList1 },
    new PickList(){ InternalName = "PickList2", DisplayName = "PickList2", Values = crmList2 }
  };
  Mock.Get(_fakeCrmProvider).Setup(x => x.GetPickLists()).Returns(crmPickLists);
  Mock.Get(_fakeSyncEngine).SetupGet(x => x.CrmProvider).Returns(_fakeCrmProvider);

  // set picklists for cache provider
  var cacheList1 = new List<string>() { "AAA", "BBB", "CCC" };
  var cacheList2 = new List<string>() { "WWW", "XXX", "ZZZ" };
  var cachePickLists = new List<PickList>() {
    new PickList(){ InternalName = "PickList1", DisplayName = "PickList1", Values = cacheList1 },
    new PickList(){ InternalName = "PickList2", DisplayName = "PickList2", Values = cacheList2 }
  };
  Mock.Get(_fakeCacheProvider).Setup(x => x.GetPickLists()).Returns(cachePickLists);
  Mock.Get(_fakeSyncEngine).SetupGet(x => x.CacheProvider).Returns(_fakeCacheProvider);

  // act
  _fakeSyncEngine.SyncPickLists();

  // assert
  Mock.Get(_fakeLoggingProvider).Verify(x => x.LogBeginPicklistSync(), Times.Once());
  Mock.Get(_fakeLoggingProvider).Verify(x => x.LogEndPicklistSync(), Times.Once());
}
  • 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-27T13:49:10+00:00Added an answer on May 27, 2026 at 1:49 pm

    First of all you never mock the class under test. In this case you are testing SyncEngine, so you should not mock it.

    If everything is mocked, you aren’t testing anything. Mocks are there to abstract away dependencies in your tests and to facilitate writing tests.

    You abstract away dependencies to:

    • Concentrate your tests on the code you want to test
    • Abstract away dependencies on things like databases, file systems,
      other processes, etc.
    • Simplify tests by reducing the code needed to test a particular feature or by easily getting a desired value from an object.

    Secondly, always mock things for a reason, always consider if each class should or should not be mocked.

    Third, it is considered bad practice by some (including myself) to test private methods. BuildPickListUpdatePlan is private, you should rather test the public methods of your class that use BuildPickListUpdatePlan.

    Testing private methods will often require “tricks” to test correctly and result in tests that are very brittle. From my experience, these tests are more prone to needing to be changed rather than tests on the public interface or API of your class. Unless you have private methods that aren’t used (in which case they are dead code and be deleted rather than tested), all your private methods will get called by your public methods. Concentrate on testing these.

    I do not know the details of your code, so my proposition might be off, but here is what I would propose your test code could look like:

    [TestMethod]
    public void TestPickListSync() {
      // Consider if each of these should be mocked, in this case the answer seems to be yes
      var _fakeCrmProvider = new Mock<ICrmProvider>();
      var _fakeCacheProvider = new Mock<ICacheProvider>();
      var _fakeLoggingProvider = new Mock<ILoggingProvider>();
    
      // Don't mock the class under test
      var syncEngine = SyncEngine(_fakeLoggingProvider.Object, _fakeCrmProvider.Object, _fakeCacheProvider.Object);
    
      // 
      var crmList1 = new List<string>() { "AAA", "CCC", "DDD" };
      var crmList2 = new List<string>() { "WWW", "XXX", "YYY" };
      var crmPickLists = new List<PickList>() {
        new PickList(){ InternalName = "PickList1", DisplayName = "PickList1", Values = crmList1 },
        new PickList(){ InternalName = "PickList2", DisplayName = "PickList2", Values = crmList2 }
      };
    
      // Since your class under test is already using the mocks, there 
      // is no need to "mock.get", just mock the methods that will be called on the mocked objects
      _fakeCrmProvider.Setup(x => x.GetPickLists()).Returns(crmPickLists);
    
      // set picklists for cache provider
      var cacheList1 = new List<string>() { "AAA", "BBB", "CCC" };
      var cacheList2 = new List<string>() { "WWW", "XXX", "ZZZ" };
      var cachePickLists = new List<PickList>() {
        new PickList(){ InternalName = "PickList1", DisplayName = "PickList1", Values = cacheList1 },
        new PickList(){ InternalName = "PickList2", DisplayName = "PickList2", Values = cacheList2 }
      };
    
      // Since your class under test is already using the mocks, there 
      // is no need to "mock.get", just mock the methods that will be called on the mocked objects
      _fakeCacheProvider.Setup(x => x.GetPickLists()).Returns(cachePickLists);
    
    
      // act
      _syncEngine.SyncPickLists();
    
      // You can do your asserts like this
      _fakeLoggingProvider.Verify(x => x.LogBeginPicklistSync(), Times.Once());
      _fakeLoggingProvider.Verify(x => x.LogEndPicklistSync(), Times.Once());
    }
    

    I don’t have VS at hand, so I couldn’t check the syntax, but it should give you some ideas. I have replaced your comments with explanations.

    Here is some simple code that show a basic example of mocking a dependency:

    [TestMethod]
    public void SampleTestMethod() {
        // Arrange
        var mockMyLoggingClass = new Mock<IMyLogging>();
    
        var classUnderTest = new ClassUnderTest(mockMyLoggingClass.Object);
    
        mockMyLoggingClass.Setup(mock = > mock.MethodThatWillGetCalled()).Returns(someValue)
    
        // Act
        var result = classUnderTest.MethodThatWillCallLoggingClass();
    
        // Assert
        Assert.AreEqual(expectedValue, result); // we can still do normal asserts apart from the verify
        mockMyLoggingClass.Verify(mock => mock.MethodThatWillGetCalled(), Times.Once());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.