views:

215

answers:

3

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; } // BZZZT!!!
}

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?

+2  A: 

You're using the auto-properties feature of C# but instead you should go the manual route and create a backing field for the property. Once you have a backing field you can set its value in the constructor (or make it a public field and set it after you have the object, but this is a little uglier).

public class IIdentityStub : IIdentity{
    private string _name;

    public IIdentityStub(string name){
        _name = name;
    }

    public string Name { get { return _name; } }
}
Jason Punyon
And if the value isn't settable via the constructor, you have a problem with your design.
JSBangs
Excellent, thanks.
Robert Harvey
@Robert Harvey: No problem sir.
Jason Punyon
Note that "public string Name { get; private set; }" and then setting in the constructor is perfectly legal.
TrueWill
@TrueWill: Yes, but that would not match the signature of the `IIdentity` interface `Name` property.
Robert Harvey
+1  A: 

I recommend using a Mock library like NMock

juharr
True, but I figured I'd better learn how to write my own stubs first.
Robert Harvey
+2  A: 

I agree with juharr - use a mocking/isolation framework. I'd recommend Moq.

The following will print "Robert":

using System;
using System.Security.Principal;
using Moq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            var mockIdentity = new Mock<IIdentity>();
            var mockPrincipal = new Mock<IPrincipal>();

            mockIdentity.SetupGet(x => x.Name).Returns("Robert");
            mockPrincipal.SetupGet(x => x.Identity).Returns(mockIdentity.Object);

            IPrincipal myStub = mockPrincipal.Object;

            Console.WriteLine(myStub.Identity.Name);
        }
    }
}

EDIT: But if you want to do it by hand...

using System;
using System.Security.Principal;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main()
        {
            IIdentity identity =
                new IdentityStub
                    {
                        Name = "Robert",
                        AuthenticationType = "Kerberos",
                        IsAuthenticated = true
                    };

            IPrincipal principal = new PrincipalStub(identity);

            Console.WriteLine(principal.Identity.Name);  // Robert
            Console.WriteLine(principal.IsInRole(PrincipalStub.ValidRole));  // True
            Console.WriteLine(principal.IsInRole("OtherRole"));  // False
        }
    }

    public class PrincipalStub : IPrincipal
    {
        public const string ValidRole = "TestRole";

        public PrincipalStub(IIdentity identity)
        {
            Identity = identity;
        }

        public IIdentity Identity { get; private set; }

        public bool IsInRole(string role)
        {
            return role == ValidRole;
        }
    }

    public class IdentityStub : IIdentity
    {
        public string Name { get; set; }
        public string AuthenticationType { get; set; }
        public bool IsAuthenticated { get; set; }
    }
}

(The above is not a unit test, just an example of hand-rolled stubs using a bit of dependency injection.)

TrueWill