Prig icon indicating copy to clipboard operation
Prig copied to clipboard

Stub a method of the parent type

Open samasama opened this issue 8 years ago • 1 comments

I have a class B that inherits class A, with method A.Id I created the indirection settings for B non inherited methods and for B.Id

I cannot replace the body for B.Id as in:

bProxy = new PProxyB(); bProxy.Id().Body = @this => 1;

Error: PProxyB does not contain a definition for "Id"

Is there a way to cast bProxy to PProxyA?

samasama avatar Sep 19 '17 17:09 samasama

Currently, Prig doesn't support the way to use, but a workaround exists:

  1. Create the Indirection Stub Setting for A.Id.
  2. In the Indirection Stub, compare an instance of B and @this then return dummy value if it is true; otherwise @this.Id with IndirectionsContext.ExecuteOriginal. Here is the example:
[Test]
public void B_Id_should_be_callable_indirectly_but_does_not_replace_A_Id()
{
    using (new IndirectionsContext())
    {
        // Arrange
        var a = new A();
        var b = new B();
        PA.IdGet().Body = @this => ReferenceEquals(@this, b) ? 1 : IndirectionsContext.ExecuteOriginal(() => @this.Id);

        // Act
        var aid = a.Id;
        var bid = b.Id;

        // Assert
        Assert.AreEqual(42, aid);
        Assert.AreEqual(1, bid);
    }
}

Full sln is here: Issues99.zip

urasandesu avatar Sep 19 '17 22:09 urasandesu