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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:50:37+00:00 2026-06-13T13:50:37+00:00

I am using VS2010, writing unit tests with MSTest. My project uses WPF, MVVM

  • 0

I am using VS2010, writing unit tests with MSTest. My project uses WPF, MVVM and the PRISM framework. I am also using Moq to mock interfaces.

I am testing the interaction between a command and a selected item in a list. The interaction is encapsulated in a ViewModel according to the MVVM pattern. Basically, when the SelectedDatabase is set, I want the Command to raise CanExecute. I have written this test for the behaviour:

public void Test()
{
    var databaseService = new Mock<IDatabaseService>();
    var databaseFunctionsController = new Mock<IDatabaseFunctionsController>();

    // Create the view model
    OpenDatabaseViewModel viewModel
         = new OpenDatabaseViewModel(databaseService.Object, databaseFunctionsController.Object);

    // Mock up the database and its view model
    var database = TestHelpers.HelpGetMockIDatabase();
    var databaseViewModel = new DatabaseViewModel(database.Object);

    // Hook up the can execute changed event
    var resetEvent = new AutoResetEvent(false);
    bool canExecuteChanged = false;
    viewModel.OpenDatabaseCommand.CanExecuteChanged += (s, e) =>
        {
             resetEvent.Set();
             canExecuteChanged = true;
        };

    // Set the selected database
    viewModel.SelectedDatabase = databaseViewModel;

    // Allow the event to happen
    resetEvent.WaitOne(250);

    // Check that it worked
    Assert.IsTrue(canExecuteChanged,
        "OpenDatabaseCommand.CanExecuteChanged should be raised when SelectedDatabase is set");
}

On the OpenDatabaseViewModel, the SelectDatabase property is as follows:

    public DatabaseViewModel SelectedDatabase
    {
        get { return _selectedDatabase; }
        set
        {
            _selectedDatabase = value;
            RaisePropertyChanged("SelectedDatabase");
            // Update the can execute flag based on the save
            ((DelegateCommand)OpenDatabaseCommand).RaiseCanExecuteChanged();
        }
    }

And also on the viewmodel:

    bool OpenDatabaseCanExecute()
    {
        return _selectedDatabase != null;
    }

TestHelpers.HelpGetMockIDatabase() just gets a mock IDatabase with some properties set.

This test passes when I run the test from VS2010, but fails when executed as part of an automated build on the server. I put in the AutoResetEvent to try to fix the problem, but it’s had no effect.

I discovered that the automated tests were using the noisolation flag in the MSTest command line, so I removed that. However, that produced a ‘pass’ once, but a ‘fail’ the next.

I think I am missing something important in all of this, but I can’t figure out what it is. Can anyone help by telling me what I’m doing wrong?

  • 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-13T13:50:38+00:00Added an answer on June 13, 2026 at 1:50 pm

    I have found an answer to explain what was going on with this unit test. There were other complicating factors that I didn’t realise were significant at the time. I didn’t include these details in my original question because I did not think they were relevant.

    The view model described in the question of code is part of a project that is using integration with WinForms. I am hosting a PRISM shell as a child of an ElementHost. Following the answer to the question on stackoverflow How to use Prism within an ElementHost, this is added to create an appropriate Application.Current:

    public class MyApp : System.Windows.Application
    {
    }
    
    if (System.Windows.Application.Current == null)
    {
        // create the Application object
        new MyApp();
    }
    

    The above code is not exercised by the unit test in question. However, it was being exercised in other unit tests that were being run beforehand, and all were run together using the /noisolation flag with MSTest.exe.

    Why should this matter? Well, buried in the PRISM code that is called as a consequence of

    ((DelegateCommand)OpenDatabaseCommand).RaiseCanExecuteChanged();
    

    in the internal class Microsoft.Practices.Prism.Commands.WeakEventHandler is this method:

    public static DispatcherProxy CreateDispatcher()
    {
        DispatcherProxy proxy = null;
    #if SILVERLIGHT
        if (Deployment.Current == null)
            return null;
    
        proxy = new DispatcherProxy(Deployment.Current.Dispatcher);
    #else
        if (Application.Current == null)
            return null;
    
        proxy = new DispatcherProxy(Application.Current.Dispatcher);
    #endif
        return proxy;
    
    }
    

    It then uses the dispatcher to call the event handler in question:

    private static void CallHandler(object sender, EventHandler eventHandler)
    {
        DispatcherProxy dispatcher = DispatcherProxy.CreateDispatcher();
    
        if (eventHandler != null)
        {
            if (dispatcher != null && !dispatcher.CheckAccess())
            {
                dispatcher.BeginInvoke((Action<object, EventHandler>)CallHandler, sender, eventHandler);
            }
            else
            {
                eventHandler(sender, EventArgs.Empty);
            }
        }
    }
    

    So it attempts to dispatch the event on the UI thread on the current application if there is one. Otherwise it just calls the eventHandler. For the unit test in question, this led to the event being lost.

    After trying many different things, the solution I settled on was just to split up the unit tests into different batches, so the unit test above is run with Application.Current == null.

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

Sidebar

Related Questions

I am writing some unit tests in VS 2010 using Selenium and C#. I
I'm writing a simple first app using Winforms, C#, VS2010, and Entity Framework. Basically,
I am using VS2010, working on a WinForms application that uses the ReportViewer control
I was happily using VS2010 Premium version for a C/C++ coding project, and needed
When writing unit tests, there are cases where one can create an Assert for
I'm writing a windows application using VS2010 and C# 4.0. I have a MDI
I'm writing a custom control (using VS2010 & C#) and it has a property
I'm writing a Windows Forms app, VS2010, NET Framework 4.0, coding in VB. I'm
Right now I am writing a little c++ console program using VS2010 Express. The
...using vs2010 and automating office 2007 The merge works fine but the saved document(pathToDestinationFile)

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.