Prig
Prig copied to clipboard
Stub a method of the parent type
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?
Currently, Prig doesn't support the way to use, but a workaround exists:
- Create the Indirection Stub Setting for
A.Id. - In the Indirection Stub, compare an instance of
Band@thisthen return dummy value if it is true; otherwise@this.IdwithIndirectionsContext.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