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

Cannot shim generic method on abstract base class #46

Open
Miista opened this issue Mar 5, 2024 · 2 comments
Open

Cannot shim generic method on abstract base class #46

Miista opened this issue Mar 5, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@Miista
Copy link
Owner

Miista commented Mar 5, 2024

MVE:

// Classes
abstract class AbstractBase
{
    public virtual T GetTFromAbstractBase<T>(string input) where T : class => null;
}

class DerivedFromAbstractBase : AbstractBase {}

// Test
var shim = Shim
    .Replace(() => Is.A<AbstractBase>().GetTFromAbstractBase<string>(Is.A<string>()))
    .With((AbstractBase @this, string @string) => "Hello");

PoseContext.Isolate(() =>
  {
      var instance = new DerivedFromAbstractBase();
      var result = instance.GetTFromAbstractBase<string>("");
  },
  shim
);

Rewriting GetTFromAbstractBase results in System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (0x8007000B).

This issue was discovered during #34

@Miista Miista added the bug Something isn't working label Mar 5, 2024
@mamen
Copy link

mamen commented Apr 15, 2024

I get the same exception when a method is called on a mocked dependency:

class Foo {

    public Foo(ISomeDependency dep) {
        _dep = dep;
    }

    public void DoSomething() {
        _dep.DoSomething<string>("foo");
    }
}

class FooTests {

    void TestForDoSomething() {
        var fixture = new Foo(MockedDependency); 

        PoseContext.Isolate(() =>
        {
            actual = fixture.DoSomething();
        }, shim);
    }
}

Is this the same problem, because I am also using a generic method (DoSomething<string>()) inside an instance method or should I open a new issue?

@Miista
Copy link
Owner Author

Miista commented Apr 21, 2024

@mamen It might be. It could be related to how the constrained instruction is handled in the rewrite process (see #49). I believe that the current implementation is incorrect in that it never executes the replacement method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants