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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:12:16+00:00 2026-06-13T02:12:16+00:00

I have an issue regarding how one would go about designing an application suited

  • 0

I have an issue regarding how one would go about designing an application suited for unit-testing.

I am trying to implement the SRP (Single-Responsibility Principle), and from what I understood this involves splitting out most functionality in seperate, dedicated classes to keep code more organised. For example, I have this specific scenario.

A class RecurringProfile, which has a method .ActivateProfile(). What this method does is mark the status as activated, and create the next (first) recurring payment for the next due date. I was going to split out the functionality to create the next recurring payment in a seperate class, for example RecurringProfileNextPaymentCreator. My instant idea is to have this class take the 'RecurringProfile' as a parameter in it’s constructor:

RecurringProfileNextPaymentCreator(IRecurringProfile profile)

However, I think this would be problematic for unit-testing. I would like to create a unit-test which tests out the ActivateProfile functionality. This method would get an instance of an IRecurringProfileNextPaymentCreator via dependency injection (Ninject), and call the method .CreateNextPayment.

My idea to create a unit-test was to create a mock of an IRecurringProfileNextPaymentCreator, and substitue that so that I can verify that the .ActivateProfile() actually called the method. However, due to the constructor parameter, this would not fit as a default constructor for NInject. Having to create a custom NInject provider just for such a case (where I can have many such classes all over the solution) would be a bit overkill.

Any ideas / best practices how one would go about this?

—
Below is a sample code regarding the above example: (Please note that the code is hand-written, and is not syntactically 100% correct)

public class RecurringProfile
{
    public void ActivateProfile()
    {
        this.Status = Enums.ProfileStatus.Activated;
        //now it should create the first recurring payment
        IRecurringProfileNextPaymentCreator creator = NInject.Get<IRecurringProfileNextPaymentCreator>();
        creator.CreateNextPayment(this); //this is what I'm having an issue about 
    }
}

And a sample unit-test:

public void TestActivateProfile()
{   
    var mockPaymentCreator = new Mock<IRecurringProfileNextPaymentCreator>();
    NInject.Bind<IRecurringProfileNextPaymentCreator>(mockPaymentCreator.Object);

    RecurringProfile profile = new RecurringProfile();
    profile.ActivateProfile();
    Assert.That(profile.Status == Enums.ProfileStatus.Activated);
    mockPayment.Verify(x => x.CreateNextPayment(), Times.Once());

}

Going up to the sample code, my issue is whether it is a good practice to pass over the RecurringProfile as a parameter to the creator.CreateNextPayment() method, or whether it makes more sense to somehow pass the RecurringProfile to the DI-framework, when getting an instance of an IRecurringProfileNextPaymentCreator, considering that the IRecurringProfileNextPaymentCreator will always act on an IRecurringProfile to create the next payment. Hope this makes the question a bit more clear.

  • 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-13T02:12:18+00:00Added an answer on June 13, 2026 at 2:12 am

    As you did not show any code I’m guessing that you want to do something like this

    public class RecurringProfile
    {
      private readonly DateTime _dueDate;
      private readonly TimeSpan _interval;
      public RecurringProfile(DateTime dueDate, TimeSpan interval)
      {
        _dueDate = dueDate;
        _interval = interval;
      }
      public bool IsActive { get; private set; }
      public DateTime DueDate
      {
        get { return _dueDate; }
      }
      public TimeSpan Interval
      {
        get { return _interval; }
      }
      public RecurringProfile ActivateProfile()
      {
        this.IsActive = true;
        return new RecurringProfile(this.DueDate + this.Interval, this.Interval);
      }
    }
    

    Isn’t that simple enough?


    Update

    Don’t abuse a DI container as a ServiceLocator. Your idea to inject the payment creator as ctor parameter is the right way to go. ServiceLocator is considered an anti-pattern in modern application architecture. Something like the code below should work fine.

    [TestClass]
    public class UnitTest1
    {
      [TestMethod]
      public void TestMethod1()
      {
        var mock = new Mock<INextPaymentCreator>();
        DateTime dt = DateTime.Now;
        var current = new RecurringProfile(mock.Object, dt, TimeSpan.FromDays(30));
        current.ActivateProfile();
        mock.Verify(c => c.CreateNextPayment(current), Times.Once());
      }
    }
    public class RecurringProfile
    {
      private readonly INextPaymentCreator _creator;
      private readonly DateTime _dueDate;
      private readonly TimeSpan _interval;
      public RecurringProfile(INextPaymentCreator creator, DateTime dueDate, TimeSpan interval)
      {
        _creator = creator;
        _dueDate = dueDate;
        _interval = interval;
      }
      public bool IsActive { get; private set; }
      public DateTime DueDate
      {
        get { return _dueDate; }
      }
      public TimeSpan Interval
      {
        get { return _interval; }
      }
      public RecurringProfile ActivateProfile()
      {
        this.IsActive = true;
        var next = this._creator.CreateNextPayment(this);
        return next;
      }
    }
    
    public interface INextPaymentCreator
    {
      RecurringProfile CreateNextPayment(RecurringProfile current);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen a few posts regarding this issue but not one specific to
I have an issue regarding an recursive count which works well in SQL server,
I have an issue regarding to auto populating a select dropdown from jQuery/JSON data
I have an issue regarding Sendkeys Class, as i want to use this class
I have recently run into a particularly sticky issue regarding committing the result of
I have a follow-up question regarding an issue I previously had on SO here
I have gone through many posts on SO regarding this issue: Tried everything in
I have looked at the different questions regarding this issue, but couldn't find anything
I know there are tons of threads regarding this issue but I have not
Please help regarding the following issue. I have enabled the Block popup option in

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.