Hi Wilson,
Generally I wouldn't test the showForm code directly (we can assume WinForms will do the right thing, and confirm it in manual or automated UI testing). Instead we can try and test ClassA in isolation from the UI framework details. I think the most common way to do this is to use a dependency to show the additional form, and then test that is called properly from ClassA.
Approximate code:
public interface ISomeFormLauncher {
void Show(Prop1 p1, Prop2 p2);
}
class ClassA {
ISomeFormLauncher launcher
public ClassA(ISomeFormLauncher launcher) { this.launcher = launcher; }
public void ShowForm() {
launcher.Show(SomeObject1, SomeObject2);
}
}
[Test]
public void ShouldShowForm() {
var launcher = Substitute.For<ISomeFormLauncher>();
var sut = new ClassA(launcher);
// ... setup ...
launcher.Received().Show(expectedObj1, expectedObj2);
}
// Tested via UI (manual or automated).
public class SomeFormLauncher : ISomeFormLauncher {
public void Show(Prop1 p1, Prop2 p2) {
SomeForm someForm = new SomeForm(); someForm.prop1 = p1;
someForm.prop2 = p2; someForm.Show();
}
}