Here's the repro code to test against:
public class SomeClassThatUsesLegacyClass
{
public static void CallTheLegacyMethod()
{
LegacyClass legacy = new LegacyClass();
//These nomrally come from properties in the class
string id = "XYZ";
int? someNumberAsNullable = 5;
DateTime date1 = DateTime.Today;
//These are output variables set by legacy method
string someOutName1;
int? someOutNumber1;
long? someOutNumber2;
MessageClass messages;
bool result = legacy.LegacyDeleteMethod(id, someNumberAsNullable, date1, false, "DELETED FROM APP1", out someOutName1, out someOutNumber1, out someOutNumber2, out messages);
if (!result)
{
if (messages != null)
{
throw new Exception("An exception occurred: " + messages.SomeMessage2);
}
else
{
throw new Exception("Legacy method was not successful but didn't tell us why.");
}
}
}
}
public class MessageClass
{
public string SomeMessage1 { get; set; }
public string SomeMessage2 { get; set; }
public string SomeMessage3 { get; set; }
public string SomeMessage4 { get; set; }
}
public class LegacyClass
{
public bool LegacyDeleteMethod(string SOME_ID, System.Nullable<int> SOME_NUMBER, System.DateTime SOME_DATE1, bool ARCHIVE, string COMMENT, out string SOME_NAME, out System.Nullable<int> SOME_OTHER_NUMBER, out System.Nullable<long> SOME_OTHER_ID, out MessageClass Exception)
{
throw new NotImplementedException("Real code would go here");
}
}
Here's the test that fails...if you walk through you'll see we're going into the 'else' because the out parameter is null.
[Isolated(), TestMethod()]
public void TestMethod1()
{
//Arrange
MessageClass message = new MessageClass()
{
SomeMessage1 = "TEST1",
SomeMessage2 = "TEST2",
SomeMessage3 = "TEST3",
SomeMessage4 = "TEST4"
};
int? someNumberAsNullable = 5;
DateTime date1 = DateTime.Today;
string someOutName1;
int? someOutNumber1;
long? someOutNumber2;
LegacyClass legacyFake = Isolate.Fake.Instance<LegacyClass>();
Isolate.WhenCalled(() => legacyFake.LegacyDeleteMethod("", someNumberAsNullable, date1, false, "", out someOutName1, out someOutNumber1, out someOutNumber2, out message));
Isolate.Swap.NextInstance<LegacyClass>().With(legacyFake);
//Act
try
{
SomeClassThatUsesLegacyClass.CallTheLegacyMethod();
Assert.Fail("Expected message");
}
catch(Exception ex)
{
//Assert
Assert.IsTrue(ex.Message.Contains(message.SomeMessage2));
}
}