locked
DispatcherTimer.Ticks do not get fired RRS feed

  • Question

  • Hi,

    I have a viewmodel, which is using a DispatcherTimer to fire the CanExecuteChanged event for several commands.
    This Timer works nice on during runtime, but in my unit test it does not fire the Ticks event.

    I am already using "SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());"
    in the [TestInitialize] method, but without success.

    What is it, I am doing wrong?

    Regards
    Rainer

    Friday, August 16, 2013 12:26 PM

Answers

  • I have found a solution to my problem:

    Shrinand.Net describes what to do to unit test classes using Dispatcher.

    Regards
    Rainer

    • Marked as answer by Rainer Queck Saturday, September 14, 2013 9:57 AM
    Saturday, September 14, 2013 9:57 AM

All replies

  • Hello,

    Thank you for posting in MSDN forum.

    Just to make the issue clearly:

    1. Which type of project are you unit testing?
    2. Whether you are only using Unit Test Framework rather than other third-party framework to do unit test for a viewmodel?
    3. Which version of Visual Studio are you using?
    4. Whether you are unit testing a ViewModel’s a async method?
    5. Whether you get some errors after you run your test? If yes, you can post here so that we can further look at this issue.
    6. How do you know that the Ticks event is not fired during unit test run?

    If you are unit testing against Silverlight project with VS2012, you need to install this extension tool:

    http://visualstudiogallery.msdn.microsoft.com/caca1e81-becb-41e4-9110-bc247f3f400b?SRC=Home

    And I find some links about unit test a ViewModel. There are some code samples within them. You can refer to them to check if they can provide some ideas about how to unit test a ViewModel:

    1. Unit Test a DispatcherTimer in a ViewModel:

    http://social.msdn.microsoft.com/Forums/silverlight/en-US/07f1d03a-0ed9-4cd9-8110-d9b948a6690e/sl-unit-test-framework-and-using-a-dispatchertimer

    2. Unit Test the ViewModel: http://stackoverflow.com/questions/4845332/unit-testing-the-viewmodel

    3. Unit Test a ViewModel’s async method: http://stackoverflow.com/questions/7260715/async-ctp-unit-testing-a-viewmodels-async-method

    http://stackoverflow.com/questions/15631635/how-to-unit-test-a-viewmodel-with-async-method

    If no help, since it is hard for us to repro this issue, maybe you could share us a sample, we try to run it in our PC. Please attach your Visual Studio project, you can upload it to the sky drive, and then share the download link in your post.

    Thanks for your understanding.

    Best regards,


    Amanda Zhu <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, August 19, 2013 5:04 AM
  • Hello Amanda,

    thanks for replying to my question!

    1. Which type of project are you unit testing?
      -> It is a .Net4.5 WPF user control library for a wpf application
    2. Whether you are only using Unit Test Framework rather than other third-party framework to do unit test for a viewmodel?
      -> MS-Test
    3. Which version of Visual Studio are you using?
      -> VS2012 Update 3
    4. Whether you are unit testing a ViewModel’s a async method?
      -> no async methods. I am only using a DispatcherTimer:
    private void InitializeMonitorTimer()
    {
        this.monitorTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
        this.monitorTimer.Tick += (sender, args) =>
        {
            this.LoadCycleCount = this.loadCycles.ActualValue;
            this.FSearchCommand.RaiseCanExecuteChanged();
            this.StartCommand.RaiseCanExecuteChanged();
            this.StopCommand.RaiseCanExecuteChanged();
        };
    
        this.monitorTimer.Start();
    }
    1. Whether you get some errors after you run your test? If yes, you can post here so that we can further look at this issue.
      -> no other errors, my unit test only runs into a timeout (which definately is set long enough):
    [TestMethod]
    public void Any_command_excpet_accept_CanExecute_raises_canExecuteChanged_if_pcbits_PreloadFeedManual_is_changed()
    {
        //Assert.Inconclusive("warte noch auf antwort bezüglich DispatcherTimer...");
        // Arrange            
        var fsearchCanExecuteChanged = 0;
        var startCanExecuteChanged = 0;
        var stopCanExecuteChanged = 0;
    
        this.pcbits.Stub(x => x.PreloadFeedManual).PropertyBehavior();
        this.pcbits.PreloadFeedManual = false;
    
        this.viewModel.FSearchCommand.CanExecuteChanged += (sender, args) => fsearchCanExecuteChanged++;
        this.viewModel.StartCommand.CanExecuteChanged += (sender, args) => startCanExecuteChanged++;
        this.viewModel.StopCommand.CanExecuteChanged += (sender, args) => stopCanExecuteChanged++;
    
        // Act
        this.pcbits.PreloadFeedManual = true;
        var timeStamp = DateTime.Now;
        while (fsearchCanExecuteChanged == 0)
        {
            var dt = DateTime.Now - timeStamp;
            Assert.IsTrue(dt.TotalSeconds < 10);
        }
    
        Thread.Sleep(150);
        this.pcbits.PreloadFeedManual = false;
        Thread.Sleep(150);
    
        // Assert
        Assert.AreEqual(2, fsearchCanExecuteChanged);
        Assert.AreEqual(2, startCanExecuteChanged);
        Assert.AreEqual(2, stopCanExecuteChanged);
    }
    1. How do you know that the Ticks event is not fired during unit test run?
      -> Running into the timeout, setting a breakpoint und running the test in debug.

    I hope the answers and the code samples help to clarify the issue.

    Regards
    Rainer


    Monday, August 19, 2013 8:18 AM
  • Hello,

    Glad to receive your reply,

    Since the only error you get is timeout, I suggest that you try to increase the Test Timeouts step by step in .testsettings file to check if you will always get this issue no matter what Timeout value you set or the issue will disappear at a specific value.

    Since Unit test project has no .testsettings file by default in VS2012, please follow up the article below to make sure that you have added the .testsettings file and apply it to unit test project correctly:

    Specifying Test Settings for Visual Studio Tests

    In addition, if possible, you can try the same scenario on another machine to check the result.

    Best regards,


    Amanda Zhu <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, August 20, 2013 2:17 AM
  • Hello Amanda,

    please take a closer look to the code I posted above (InitializeMonitorTimer)

    I get the timeout "BECAUSE" the DispatcherTimer is not firing in the UnitTest environment!
    The Timer is set to 100 milliseconds therefore within 10 Seconnds it should fire about 100 times!
    The test is only looking for 2 times.

    AND....

    At runtime the code under test works very well.

    Regards
    Rainer

    Wednesday, August 21, 2013 4:41 AM
  • Hi,

    I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience.

    Best regards,


    Amanda Zhu <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, August 21, 2013 9:09 AM
  • Hello Amanda,

    thanks for not giving up ;-)

    Regards
    Rainer

    Wednesday, August 21, 2013 9:22 AM
  • Hi Rainer,

    How about you debug that test method, is DispatcherTimer.Ticks event triggered?

    In addition, based on the code you provided above, it seems that you are working on a Composite Application for WPF. Could you please send me a very simple project so that I can quicker reproduce your issue? You can upload it to the SkyDrive.

    Thanks.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Thursday, August 22, 2013 5:22 AM
  • Hi Vicky,

    As mentioned above, debugging the Test shows that the timer does not get fired.

    I currently have no time to prepare a simple project, since I am preparing for some traveling.
    I have to leave to morrow and that is why won't be able to respond to this thread for the next 3 Weeks.

    I am looking forward to hear of a solution/more hints by the time being back ;-)

    Regards
    Rainer

    Thursday, August 22, 2013 3:04 PM
  • I have found a solution to my problem:

    Shrinand.Net describes what to do to unit test classes using Dispatcher.

    Regards
    Rainer

    • Marked as answer by Rainer Queck Saturday, September 14, 2013 9:57 AM
    Saturday, September 14, 2013 9:57 AM