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

  • Home
  • SEARCH
  • 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 8227791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:14:45+00:00 2026-06-07T16:14:45+00:00

I’ve got a class that has a private member that has for type System.Windows.Forms.Timer

  • 0

I’ve got a class that has a private member that has for type System.Windows.Forms.Timer. There’s also a private method that is being called every time my timer ticks.

  1. Is it worth testing the method? (since it’s private)
  2. How can I test it? (I know I can have my test class inheriting the class I want to test…)
  3. Should I be mocking my timer? Because if I have to test a class that uses an internal timer, my tests may take a lot of time to complete, right?

edit:

Actually, the method has a dependency on timing, here’s the code:

private void alertTick(object sender, EventArgs e) {
    if (getRemainingTime().Seconds <= 0) {
        Display.execute(Name, WarningState.Ending, null);
        AlertTimer.Stop();
    }
    else {
        var warning = _warnings.First(x => x == getRemainingTime());

        if (warning.TotalSeconds > 0)
            Display.execute(Name, WarningState.Running, warning);
    }
}

As you can see, if the timer is running, it calls Display.execute() with different parameters from when it’s ending (when the remaining time equals 0). Would that be a problem of design?

  • 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-07T16:14:48+00:00Added an answer on June 7, 2026 at 4:14 pm
    1. You are not testing methods (private or public) – you are verifying behavior of your class. And if you have not verified some behavior, then you can’t tell it was implemented. There are several ways this behavior could be invoked – public interface of your class, or some event in dependency. Also not necessary that behavior invocation will change something reached by the public interface, interactions with dependencies also matter.
    2. See example below – it shows how to test such “hidden” behavior.
    3. See example below – it shows how to split responsibilities, inject dependencies and mock them.

    Actually your class have too many responsibilities – one is scheduling some task, and another – executing some actions. Try to split your class into two separate classes with single responsibilities.

    So, scheduling goes to scheduler 🙂 API of scheduler could be like:

    public interface IScheduler
    {
        event EventHandler<SchedulerEventArgs> Alarm;
        void Start();
        void Stop();
    }
    

    Forget about scheduler for now. Return and implement your second class, which will display some warnings. Let’s go test first (with Moq):

    [Test]
    public void ShouldStopDisplayingWarningsWhenTimeIsOut()
    {
        Mock<IDisplay> display = new Mock<IDisplay>();
        Mock<IScheduler> scheduler = new Mock<IScheduler>();                      
    
        Foo foo = new Foo("Bar", scheduler.Object, display.Object);
        scheduler.Raise(s => s.Alarm += null, new SchedulerEventArgs(0));
    
        display.Verify(d => d.Execute("Bar", WarningState.Ending, null));
        scheduler.Verify(s => s.Stop());
    }
    

    Write implementation:

    public class Foo
    {
        private readonly IScheduler _scheduler;
        private readonly IDisplay _display;
        private readonly string _name;
    
        public Foo(string name, IScheduler scheduler, IDisplay display)
        {
            _name = name;
            _display = display;
            _scheduler = scheduler;
            _scheduler.Alarm += Scheduler_Alarm;
            _scheduler.Start();
        }
    
        private void Scheduler_Alarm(object sender, SchedulerEventArgs e)
        {
            _display.Execute(_name, WarningState.Ending, null);
            _scheduler.Stop();
        }
    }
    

    Test passes. Write another one:

    [Test]
    public void ShouldNotStopDisplayingWarningsWhenTimeRemains()
    {
        Mock<IDisplay> display = new Mock<IDisplay>(MockBehavior.Strict);
        Mock<IScheduler> scheduler = new Mock<IScheduler>(MockBehavior.Strict);
        scheduler.Setup(s => s.Start());
    
        Foo foo = new Foo("Bar", scheduler.Object, display.Object);
        scheduler.Raise(s => s.Alarm += null, new SchedulerEventArgs(1));
    }
    

    Test failed. Ah, you need condition for remaining time:

    private void Scheduler_Alarm(object sender, SchedulerEventArgs e)
    {
        if (e.RemainingTime > 0)
            return;
    
        _display.Execute(_name, WarningState.Ending, null);
        _scheduler.Stop();
    }
    

    You can continue writing tests for your class, which responsible for handling scheduler alerts and executing some warnings on display. When you finish, you can write implementation for your IScheduler interface. It does not matter how you will implement scheduling – via System.Windows.Forms.Timer or via System.ThreadingTimer, or some other way.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am doing a simple coin flipping experiment for class that involves flipping a
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I know there's a lot of other questions out there that deal with this
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.