pose icon indicating copy to clipboard operation
pose copied to clipboard

TargetInvocationException: Common Language Runtime detected an invalid program thrown with DateTime.Now replacement

Open Dreamwalker666 opened this issue 6 years ago • 3 comments

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 )

Dreamwalker666 avatar Sep 24 '18 10:09 Dreamwalker666

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.

markdemich avatar Oct 22 '18 23:10 markdemich

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.

watsonsong avatar Jan 16 '19 09:01 watsonsong

I think the issue happens when we invoke var result = test.YearBeforeNow(); Is there any update on this issue?

sushenasree avatar Jun 14 '21 10:06 sushenasree