Skip to content

.NET 6.0 #71

Description

@hvkooten

Warning: Package 'Mono.Reflection 1.1.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework 'net6.0'. This package may not be fully compatible with your project.

Error: System.InvalidProgramException: Common Language Runtime detected an invalid program.
at stub_ctor_System.Runtime.CompilerServices.DefaultInterpolatedStringHandler_.ctor(DefaultInterpolatedStringHandler& , Int32 , Int32 , RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_TestPoseShimming.MyClass_DoSomething(MyClass )
at stub_TestPoseShimming.MyClass_DoSomething(MyClass , RuntimeMethodHandle , RuntimeTypeHandle )
at dynamic_TestPoseShimming.UnitTest1+<>c_<SetCetSessionId_Test>b__0_2(<>c )

Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at Pose.PoseContext.Isolate(Action entryPoint, Shim[] shims)
at TestPoseShimming.UnitTest1.SetCetSessionId_Test() in C:\Sources\Temp\TestPoseShimming\TestPoseShimming\UnitTest1.cs:line 19
at System.Threading.Tasks.Task.<>c.b__128_0(Object state)
at Xunit.Sdk.AsyncTestSyncContext.<>c__DisplayClass7_0.b__0() in C:\Dev\xunit\xunit\src\xunit.execution\Sdk\AsyncTestSyncContext.cs:line 58

using Pose;
using System;
using Xunit;

namespace TestProject1
{
public class UnitTest1
{
[Fact]
public void test1()
{
Shim consoleShim = Shim.Replace(() => Console.WriteLine(Is.A())).With(
delegate (string s) { Console.WriteLine("Hijacked: {0}", s); });

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

        Shim setterShim = Shim.Replace(() => Console.Title, true).With((string title) => { Console.Title = "My Title"; });

        Shim classPropShim = Shim.Replace(() => Is.A<MyClass>().MyProperty).With((MyClass @this) => 100);

        classPropShim = Shim.Replace(() => Is.A<MyClass>().MyProperty, true).With((MyClass @this, int prop) => { @this.MyProperty = prop * 10; });

        Shim ctorShim = Shim.Replace(() => new MyClass()).With(() => new MyClass() { MyProperty = 10 });

        Shim classShim = Shim.Replace(() => Is.A<MyClass>().DoSomething()).With(
            delegate (MyClass @this) { Console.WriteLine("doing someting else"); });

        MyClass myClass = new MyClass();
        Shim myClassShim = Shim.Replace(() => myClass.DoSomething()).With(
            delegate (MyClass @this) { Console.WriteLine("doing someting else with myClass"); });

        // This block executes immediately
        PoseContext.Isolate(() =>
        {
            // All code that executes within this block
            // is isolated and shimmed methods are replaced

            // Outputs "Hijacked: Hello World!"
            Console.WriteLine("Hello World!");

            // Outputs "4/4/04 12:00:00 AM"
            Console.WriteLine(DateTime.Now);

            // Outputs "doing someting else"
            new MyClass().DoSomething();

            // Outputs "doing someting else with myClass"
            myClass.DoSomething();

        }, consoleShim, dateTimeShim, classPropShim, classShim, myClassShim);
    }

    [Fact]
    public void Test2()
    {
        // ARRANGE
        Shim classPropShim = Shim.Replace(() => Is.A<MyClass>().MyProperty).With((MyClass @this) => 100);
        PoseContext.Isolate(() =>
        {
            new MyClass().DoSomething();
        }, classPropShim);
    }
}

class MyClass
{
    public int MyProperty { get; set; }
    public void DoSomething() => Console.WriteLine($"doing someting: {MyProperty}");
}

}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions