开发者

Making a class testable

So I have a class called User. It has an internal constructor. I want to create a User object though so I can mock it like this:

public ISessionManagerInstance MockedSessionManager()
{
    var manager = new Mock<ISessionManagerInstance>();
    var company = new Chatham.Web.Business.Classes.Company(500, "", "", Enumerations.WebRelationshipInfo.NotSet, "", 0, 0, Data.Login.TeamOwnership.NotSet, 0, 0, false, null, false);
    manager.Setup(p => p.Company).Returns(company);

    Chatham.Web.Business.Classes.User displayUser = typeof(Chatham.Web.Business.Classes.User);
    displayUser.E开发者_C百科ntityID = 1786;
    manager.Setup(p => p.DisplayUser).Returns(displayUser);

    return manager.Object;
}

Now, Company has a constructor, so that's easy. But User has one that's only internal. Is there any way I can create a User and just set one int property on it so I can pass that object back on the mock?


I can think of two possibilities:

create an IUser interface, have User implement it and create mocks against it. This is a very common practice in the world of .NET testing. All of your methods that use Users will mostly likely need to now accept IUser references.

Another possibility (which I don't recommend, but it's there) is to use the InternalsVisibleTo assembly attribute. Then the internals of your production assembly can be visible from your test assembly.


One option would be to add your Unit Test Project as an "internal to" of your main project, this allows your unit test code access to things marked "internal" without allowing anyone else to do so. Its a simple thing to implement in the AssemblyInfo.cs file:

// main project AssemblyInfo.cs file 
[assembly: InternalsVisibleTo("YourProject.Tests")]


You could use the InternalsVisibleTo property in your AssemblyInfo.cs to make internal members visible to your UnitTest assembly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜