urasandesu / Prig

Prig is a lightweight framework for test indirections in .NET Framework.
Other
117 stars 21 forks source link

Stub a method of the parent type #99

Open samasama opened 7 years ago

samasama commented 7 years ago

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?

urasandesu commented 7 years ago

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