Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When shimming DateTimeOffset access to the .Date property throws "Object reference not set to an instance of an object" #51

Open
douglasg14b opened this issue Jun 22, 2024 · 2 comments
Assignees
Labels
question/investigation Further information or investigation is required

Comments

@douglasg14b
Copy link

It seems that something is messed up here.

Using Poser v2.1.0

        Shim dateTimeShim = Shim.Replace(() => DateTimeOffset.UtcNow)
            .With(() => DateTimeOffset.Parse(startDate, styles: DateTimeStyles.AssumeUniversal).AddMonths(monthIndex));

        PoseContext.Isolate(() =>
            {
                bool test2 = DateTimeOffset.UtcNow.Date > DateTimeOffset.UtcNow.AddMonths(1).Date;
            },
            dateTimeShim);
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at impl_System.DateTimeOffset_AddMonths(DateTimeOffset&, Int32)
   at stub_call_System.DateTimeOffset_AddMonths(DateTimeOffset&, Int32)
   at impl_Buzzscreens.Features.Tests.Orders.OrderTests+<>c_<WithOrder_NextBillingDate_ReturnsCorrectDate>b__1_4(<>c)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
   --- End of inner exception stack trace ---
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
   at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at Pose.PoseContext.Isolate(Action entryPoint, Shim[] shims)
   at Buzzscreens.Features.Tests.Orders.OrderTests.WithOrder_NextBillingDate_ReturnsCorrectDate(String startDate, String endDate, Int32 monthIndex, String expectedEnd, Boolean shouldEndBeLastMonth) in C:\Users\Douglas\Documents\01 Programming\02 Contracts\buzzscreens-app\apps\backend\Buzzscreens.Features.Tests\Orders\OrderTests.cs:line 60
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithManyArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

The following works as expected:

        DateTime test = DateTimeOffset
            .Parse(startDate, styles: DateTimeStyles.AssumeUniversal)
            .AddMonths(monthIndex)
            .Date;
@douglasg14b
Copy link
Author

douglasg14b commented Jun 22, 2024

Similar thing happens with the example DateTime shim:

        Shim dateTimeShim = Shim.Replace(() => DateTime.UtcNow)
            .With(() => new DateTime(2004, 4, 4));

DateTimeOffset.UtcNow.AddMonths(1); throws something similar:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at impl_System.DateTimeOffset_AddMonths(DateTimeOffset&, Int32)
   at stub_call_System.DateTimeOffset_AddMonths(DateTimeOffset&, Int32)
   at impl_Buzzscreens.Features.Tests.Orders.OrderTests+<>c_<WithOrder_NextBillingDate_ReturnsCorrectDate>b__1_4(<>c)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)

DateTime.UtcNow.AddMonths(1); throws no such errors.

@Miista
Copy link
Owner

Miista commented Jul 1, 2024

I tried reproducing locally using .NET 8.0. I did not encounter any exception. What is your target framework?

Additionally, what are the values of startDate and monthIndex?

@Miista Miista self-assigned this Jul 1, 2024
@Miista Miista added the question/investigation Further information or investigation is required label Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question/investigation Further information or investigation is required
Projects
None yet
Development

No branches or pull requests

2 participants