In my application the user can create new methods and edit them. I’ve created a MSTest Unit test to verify the MethodViewModel behaves correctly.
Current test is as follows:
[TestMethod]
public void Create_new_method_edit_and_apply()
{
var methodViewModel = new MethodViewModel();
// Initial state: Command 'New' can be executed
Assert.IsTrue(methodViewModel.CmdNew.CanExecute(null));
// Initial state: Command 'Ok' can *not* be executed
Assert.IsFalse(methodViewModel.CmdOk.CanExecute(null));
// Initial state: There is no current method
Assert.IsNull(methodViewModel.CurrentMethod);
// Invoke the 'New' command to start creating a new method
methodViewModel.CmdNew.Execute(null);
// 'Create new method' state: New command can *not* be executed
Assert.IsFalse(methodViewModel.CmdNew.CanExecute(null));
// 'Create new method' state: Ok command *still can't* be executed
Assert.IsFalse(methodViewModel.CmdOk.CanExecute(null));
// 'Create new method' state: setting the user input of method name
methodViewModel.UserInputMethodName = "My new method";
// 'Create new method' state: Ok command *now can* be executed
Assert.IsTrue(methodViewModel.CmdOk.CanExecute(null));
// Invoke the 'Ok' command to finish creating a new method
methodViewModel.CmdOk.Execute(null);
// 'New method created' state: Command 'New' can be executed
Assert.IsTrue(methodViewModel.CmdNew.CanExecute(null));
// 'New method created' state: Command 'Ok' can *not* be executed
Assert.IsFalse(methodViewModel.CmdOk.CanExecute(null));
// 'New method created' state: Now *there is a* current method
Assert.IsNotNull(methodViewModel.CurrentMethod);
// 'New method created' state: And also a working copy
Assert.IsNotNull(methodViewModel.CurrentMethodWorkingCopy);
// 'New method created' state: Checking some current values
Assert.AreEqual(methodViewModel.CurrentMethod.Name, "My new method");
Assert.AreEqual(methodViewModel.CurrentMethod.LinearTableSpeed, 0);
Assert.AreEqual(methodViewModel.CurrentMethodWorkingCopy.LinearTableSpeed, 0);
// Commands 'Apply' and 'Reject' *can not* be executed
Assert.IsFalse(methodViewModel.CmdApply.CanExecute(null));
Assert.IsFalse(methodViewModel.CmdReject.CanExecute(null));
// 'Editing method' state: Changing a value. NOTE: This is done in
// the working copy
methodViewModel.CurrentMethodWorkingCopy.LinearTableSpeed = 3;
// Verifying it
Assert.AreEqual(methodViewModel.CurrentMethod.LinearTableSpeed, 0);
Assert.AreEqual(methodViewModel.CurrentMethodWorkingCopy.LinearTableSpeed, 3);
// Now commands 'Apply' and 'Reject' *can* be executed
Assert.IsTrue(methodViewModel.CmdApply.CanExecute(null));
Assert.IsTrue(methodViewModel.CmdReject.CanExecute(null));
// Invoke the 'Apply' command to apply the changes
methodViewModel.CmdApply.Execute(null);
// Verify that CurrentMethod now has the changed value (as the
// working copy already had).
Assert.AreEqual(methodViewModel.CurrentMethod.LinearTableSpeed, 3);
Assert.AreEqual(methodViewModel.CurrentMethodWorkingCopy.LinearTableSpeed, 3);
// Now add another method
methodViewModel.CmdNew.Execute(null);
methodViewModel.UserInputMethodName = "2nd method";
methodViewModel.CmdOk.Execute(null);
// Checking some current values of new method
Assert.AreEqual(methodViewModel.CurrentMethod.Name, "2nd method");
Assert.AreEqual(methodViewModel.CurrentMethod.LinearTableSpeed, 0);
Assert.AreEqual(methodViewModel.CurrentMethodWorkingCopy.LinearTableSpeed, 0);
// Now select the first method again
methodViewModel.MethodsView.MoveCurrentToFirst();
// Checking if the firtly created method has still the expected values
Assert.AreEqual(methodViewModel.CurrentMethod.Name, "My new method");
Assert.AreEqual(methodViewModel.CurrentMethod.LinearTableSpeed, 3);
Assert.AreEqual(methodViewModel.CurrentMethodWorkingCopy.LinearTableSpeed, 3);
}
Now this is a bit much functionality in one test method that I’d like to separate, e.g.
MethodViewModel methodViewModel;
[TestMethod]
public void Create_new_method()
{
methodViewModel = new MethodViewModel();
...
}
[TestMethod]
public void Edit_newly_created_method()
{
Assert.IsNotNull(methodViewModel);
...
}
[TestMethod]
public void Create_another_method()
{
Assert.IsNotNull(methodViewModel);
...
}
[TestMethod]
public void Switch_back_to_first_method_and_verify()
{
Assert.IsNotNull(methodViewModel);
...
}
But then these methods must be executed in the correct order (here: as they are listed top to bottom). And the first one has to create the MethodViewModel.
However, everywhere I read that non-isolated, dependent tests are a bad thing.
So, my question is, is this really true?
And if not, what’s the best way to achieve an ordered test? I suppose it is better to change the test framework to NUnit then?
Aucun commentaire:
Enregistrer un commentaire