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

.NET 6.0 #71

Open
hvkooten opened this issue Feb 22, 2022 · 1 comment
Open

.NET 6.0 #71

hvkooten opened this issue Feb 22, 2022 · 1 comment

Comments

@hvkooten
Copy link

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}");
}

}

@Miista
Copy link

Miista commented Jan 13, 2024

@hvkooten I got a working copy of the library over at Miista/pose#2

Miista added a commit to Miista/pose that referenced this issue Jan 25, 2024
Release v2.0 to NuGet

Version 2.0 supports the following targets:
* .NET Standard 2.0
* .NET Core 2.0
* .NET Core 3.0
* .NET Framework 4.8
* .NET 7
* .NET 8

Version 2.0 fixes the following issues:
* tonerdo/pose#17
* tonerdo/pose#37
* tonerdo/pose#38
* tonerdo/pose#41
* tonerdo/pose#47
* tonerdo/pose#49
* tonerdo/pose#60
* tonerdo/pose#67
* tonerdo/pose#68
* tonerdo/pose#70
* tonerdo/pose#71
* tonerdo/pose#72
* tonerdo/pose#75
* tonerdo/pose#79
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants