Since we have a system class that ties everything together, we can now write the first test.
Add a new Unit Test Project to your solution; name it SimpleMVVMTests.
Rename the pregenerated test method to InitTest() and put the following code in it:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SimpleMVVM.ViewModels;
namespace SimpleMVVMTests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void InitTest()
{
SimpleMVVMApp app = new SimpleMVVMApp();
app.Init();
MainMenuPageVM mainWindow = (MainMenuPageVM)app.PageFrame.CurrentPage;
Assert.IsNotNull(mainWindow);
}
}
}
Note: You will have to add a reference to the main project in the test project.
This creates a new object of our system class, initializes it and tests whether the initialization has made the main menu page the current page.
Run the test – it should be green.
We could (and should) also test whether our main menu button is hidden as it’s supposed to be:
using System;
using System.Windows;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SimpleMVVM.ViewModels;
namespace SimpleMVVMTests
{
[TestClass]
public class UnitTest1 : ISystemConnector
{
private int _quitCallCounter;
[TestMethod]
public void InitTest()
{
// reset counter
_quitCallCounter = 0;
// create app
SimpleMVVMApp app = new SimpleMVVMApp(this);
// initialize app
app.Init();
// current page should be MainMenuPage
MainMenuPageVM mainMenuPage = (MainMenuPageVM)app.PageFrame.CurrentPage;
Assert.IsNotNull(mainMenuPage);
// and main menu button should be collapsed
Assert.AreEqual(Visibility.Collapsed, app.PageFrame.MainMenuButtonVisibility);
// click "Quit"
mainMenuPage.QuitCommand.Execute(null);
// Quit() should have been called exactly once
Assert.AreEqual(1, _quitCallCounter);
}
public void Quit()
{
++_quitCallCounter;
}
}
}