How do you create a unit-testing stub for an interface containing a read-only member?

Posted by Robert Harvey on Stack Overflow See other posts from Stack Overflow or by Robert Harvey
Published on 2010-03-29T18:01:36Z Indexed on 2010/03/29 18:03 UTC
Read the original article Hit count: 421

Filed under:
|
|
|
|

I am writing some unit tests for an extension method I have written on IPrincipal. To assist, I have created a couple of helper classes (some code for not-implemented members of the interfaces has been omitted for brevity):

public class IPrincipalStub : IPrincipal
{
    private IIdentity identityStub = new IIdentityStub();

    public IIdentity Identity
    {
        get { return identityStub }
        set { identityStub = value }
    }
}

public class IIdentityStub : IIdentity
{
    public string Name { get; set; }
}

However, the Name property in the IIdentity interface is read-only (the IIDentity interface specifies a getter but not a setter for the Name property).

How can I set the Name property in my stub object for testing purposes if the interface has defined it as a read-only property?

© Stack Overflow or respective owner

Related posts about c#

Related posts about interface