Hi,
I want to test this (UI) code from a login dialog:
private void btnOk_Click(object sender, RoutedEventArgs e)
{
bool isValid = checkPassword(txtUsername.Text, txtDomain.Text, pwbPassword.Password);
if (isValid)
{
this.DialogResult = true;
}
else
{
string msg = "The combination of username, password and domain is not valid, or your account is locked.";
string caption = "Login failed";
MessageBox.Show(msg, caption, MessageBoxButton.OK, MessageBoxImage.Stop);
}
}
The checkPassword() will be tested with an own test.
I started with this test code:
// Arrange
LoginWindow_Accessor target = new LoginWindow_Accessor();
object sender = null;
RoutedEventArgs e = null;
Isolate.Fake.StaticMethods<System>(Members.ReturnRecursiveFakes);
// Act
target.btnOk_Click(sender, e);
// Assert
Isolate.Verify.WasCalledWithAnyArguments(
() => System.Windows.MessageBox.Show(
string.Empty,
string.Empty,
MessageBoxButton.OK,
MessageBoxImage.Stop)
);
The text boxes in the form are empty, so checkPassword will return false and the messagebox will be called. The test is ok.
Now I want do fake the checkPassword function and first return 'false' again by adding this code (on top):
LoginWindow_Accessor fake = Isolate.Fake.Instance<LoginWindow_Accessor>();
Isolate.NonPublic.WhenCalled(fake, "checkPassword").WillReturn(false);
Isolate.Swap.NextInstance<LoginWindow_Accessor>().With(fake);
But than the test fails: "TypeMock Verification: Method System.Windows.MessageBox.Show(String, String, MessageBoxButton, MessageBoxImage) was expected but was not called"
What's wrong here?
Thanks,
Mike