|
using System;
using System.IO;
using System.Windows.Input;
using CodeProject.VisualStudio.QualityTools.UnitTestFramework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TaskList.DataModel;
using TaskList.ViewModel;
namespace Test.TaskList.ViewModel
{
[TestClass]
public class MainWindowViewModelTest
{
private class MainWindowView : IMainWindowView
{
public bool GetOpenFileNameCalled { get; set; }
public bool GetSaveAsFileNameCalled { get; set; }
#region IMainWindowView Members
public string GetOpenFileName()
{
GetOpenFileNameCalled = true;
return string.Empty;
}
public string GetSaveAsFileName()
{
GetSaveAsFileNameCalled = true;
return string.Empty;
}
#endregion
}
public MainWindowViewModelTest()
{
}
private TestContext testContextInstance;
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
private MainWindowView view;
private TaskListTestApplication application;
private MainWindowViewModel viewModel;
#region Additional test attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
[TestInitialize]
public void TestInitialize()
{
view = new MainWindowView();
viewModel = new MainWindowViewModel(view);
viewModel.TaskListApplication = application = new TaskListTestApplication();
}
#endregion
private string GetTemporaryCollectionFile()
{
string fileName = Path.GetTempFileName();
try
{
TaskCollection tasks = new TaskCollection();
Task item = new Task();
item.Title = "xyzzy";
tasks.Add(item);
tasks.Save(fileName);
return fileName;
}
catch (Exception)
{
File.Delete(fileName);
throw;
}
}
[TestMethod]
public void ApplicationCommands_SaveAs_should_be_enabled()
{
CommandTester commandTester = new CommandTester(viewModel, ApplicationCommands.SaveAs);
Specify.That(commandTester.CanExecute()).ShouldBeTrue();
}
[TestMethod]
public void ApplicationCommands_New_should_be_enabled()
{
CommandTester commandTester = new CommandTester(viewModel, ApplicationCommands.New);
Specify.That(commandTester.CanExecute()).ShouldBeTrue();
}
[TestMethod]
public void ApplicationCommands_Open_should_be_enabled()
{
CommandTester commandTester = new CommandTester(viewModel, ApplicationCommands.Open);
Specify.That(commandTester.CanExecute()).ShouldBeTrue();
}
[TestMethod]
public void ApplicationCommands_Save_should_be_enabled_if_dirty()
{
CommandTester commandTester = new CommandTester(viewModel, ApplicationCommands.Save);
viewModel.Tasks.IsDirty = true;
Specify.That(commandTester.CanExecute()).ShouldBeTrue();
}
[TestMethod]
public void ApplicationCommands_Save_should_not_be_enabled_if_not_dirty()
{
CommandTester commandTester = new CommandTester(viewModel, ApplicationCommands.Save);
Specify.That(commandTester.CanExecute()).ShouldBeFalse();
}
[TestMethod]
public void ApplicationCommands_Open_should_open_file()
{
string fileName = GetTemporaryCollectionFile();
try
{
CommandTester commandTester = new CommandTester(viewModel, ApplicationCommands.Open);
commandTester.Execute(fileName);
Specify.That(viewModel.Tasks).ShouldNotBeEmpty();
}
finally
{
File.Delete(fileName);
}
}
[TestMethod]
public void ApplicationCommands_Open_should_ask_view_for_file_name_if_not_provided()
{
CommandTester commandTester = new CommandTester(viewModel, ApplicationCommands.Open);
commandTester.Execute();
Specify.That(view.GetOpenFileNameCalled).ShouldBeTrue();
}
[TestMethod]
public void ApplicationCommands_SaveAs_should_ask_view_for_file_name_if_not_provided()
{
CommandTester commandTester = new CommandTester(viewModel, ApplicationCommands.SaveAs);
commandTester.Execute();
Specify.That(view.GetSaveAsFileNameCalled).ShouldBeTrue();
}
[TestMethod]
public void ApplicationCommands_Save_should_ask_view_for_file_name_if_new_tasks()
{
CommandTester commandTester = new CommandTester(viewModel, ApplicationCommands.Save);
viewModel.Tasks.IsDirty = true;
commandTester.Execute();
Specify.That(view.GetSaveAsFileNameCalled).ShouldBeTrue();
}
[TestMethod]
public void ApplicationCommands_Save_should_save_file()
{
string fileName = GetTemporaryCollectionFile();
try
{
CommandTester loadTester = new CommandTester(viewModel, ApplicationCommands.Open);
loadTester.Execute(fileName);
File.Delete(fileName);
viewModel.Tasks.IsDirty = true;
CommandTester saveTester = new CommandTester(viewModel, ApplicationCommands.Save);
saveTester.Execute();
Specify.That(File.Exists(fileName)).ShouldBeTrue();
}
finally
{
File.Delete(fileName);
}
}
[TestMethod]
public void ApplicationCommands_SaveAs_should_save_file()
{
string fileName = GetTemporaryCollectionFile();
try
{
CommandTester loadTester = new CommandTester(viewModel, ApplicationCommands.Open);
loadTester.Execute(fileName);
File.Delete(fileName);
CommandTester saveTester = new CommandTester(viewModel, ApplicationCommands.SaveAs);
saveTester.Execute(fileName);
Specify.That(File.Exists(fileName)).ShouldBeTrue();
}
finally
{
File.Delete(fileName);
}
}
[TestMethod]
public void ApplicationCommands_New_should_create_empty_tasks()
{
string fileName = GetTemporaryCollectionFile();
try
{
CommandTester loadTester = new CommandTester(viewModel, ApplicationCommands.Open);
loadTester.Execute(fileName);
File.Delete(fileName);
CommandTester newTester = new CommandTester(viewModel, ApplicationCommands.New);
newTester.Execute();
Specify.That(viewModel.Tasks).ShouldBeEmpty();
}
finally
{
File.Delete(fileName);
}
}
[TestMethod]
public void ApplicationCommands_New_should_clear_file_name()
{
string fileName = GetTemporaryCollectionFile();
try
{
CommandTester loadTester = new CommandTester(viewModel, ApplicationCommands.Open);
loadTester.Execute(fileName);
File.Delete(fileName);
CommandTester newTester = new CommandTester(viewModel, ApplicationCommands.New);
newTester.Execute();
Specify.That(viewModel.FileName).ShouldBeEmpty();
}
finally
{
File.Delete(fileName);
}
}
[TestMethod]
public void TaskListCommands_Exit_should_be_enabled()
{
CommandTester tester = new CommandTester(viewModel, TaskListCommands.Exit);
Specify.That(tester.CanExecute()).ShouldBeTrue();
}
[TestMethod]
public void TaskListCommands_Exit_should_call_ITaskListApplication_Exit()
{
CommandTester tester = new CommandTester(viewModel, TaskListCommands.Exit);
tester.Execute();
Specify.That(application.WasExited).ShouldBeTrue();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Windows developer with 10+ years experience working in the banking industry.