pose
pose copied to clipboard
TargetInvocationException: Common Language Runtime detected an invalid program thrown with DateTime.Now replacement
Code been used
class SimpleTest
{
public DateTime YearBeforeNow()
{
return DateTime.Now.AddYears(-1);
}
}
[Test]
public void Pose_DateTimeTest()
{
// Arrange
Shim shim = Shim.Replace(() => DateTime.Now).With(() => new DateTime(2000, 1, 1));
var test = new SimpleTest();
PoseContext.Isolate(() =>
{
// Act
var result = test.YearBeforeNow();
// Assert
var expected = new DateTime(1999, 1, 1);
Assert.AreEqual(expected, result);
}, shim);
}
Exception Details
System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation. ----> System.InvalidProgramException : Common Language Runtime detected an invalid program. at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Delegate.DynamicInvokeImpl(Object[] args) at Ssg.RiskAssessment.UnitTest.DateTimeTests.Pose() --InvalidProgramException at dynamic_System.DateTime_IsLeapYear(Int32 ) at stub_System.DateTime_IsLeapYear(Int32 , RuntimeMethodHandle , RuntimeTypeHandle ) at dynamic_System.DateTime_DaysInMonth(Int32 , Int32 ) at stub_System.DateTime_DaysInMonth(Int32 , Int32 , RuntimeMethodHandle , RuntimeTypeHandle ) at dynamic_System.DateTime_AddMonths(DateTime& , Int32 ) at stub_System.DateTime_AddMonths(DateTime& , Int32 , RuntimeMethodHandle , RuntimeTypeHandle ) at stub_System.DateTime_AddYears(DateTime& , Int32 , RuntimeMethodHandle , RuntimeTypeHandle ) at dynamic_Ssg.RiskAssessment.UnitTest.DateTimeTests+SimpleTest_YearBeforeNow(SimpleTest ) at stub_virt_Ssg.RiskAssessment.UnitTest.DateTimeTests+SimpleTest_YearBeforeNow(SimpleTest , RuntimeMethodHandle , RuntimeTypeHandle ) at dynamic_Ssg.RiskAssessment.UnitTest.DateTimeTests+<>c__DisplayClass2_0_<Pose>b__2(<>c__DisplayClass2_0 )
I just ran into something similar. I think the fact that you are Asserting from within the PoseContext causes this. The Assert throws and exception, and that's how exceptions are displayed.
I met the same error. It seems can not use the Assert and exception inside the PoseContext. I don't know whether there has a way to fix it.
I think the issue happens when we invoke var result = test.YearBeforeNow(); Is there any update on this issue?