Testable class execution

So, I have a class called User . It has an internal constructor. I want to create a User object, although I can mock it as follows:

 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.EntityID = 1786; manager.Setup(p => p.DisplayUser).Returns(displayUser); return manager.Object; } 

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

+4
source share
3 answers

I can imagine two possibilities:

create the IUser interface, ask the user to implement it and create mocks for it. This is a very common practice in the .NET testing world. All of your methods that users use will most likely now need to accept IUser links.

Another possibility (which I do not recommend, but it is there) is to use the InternalsVisibleTo attribute. Then the inside of your production assembly can be seen from your test assembly.

+6
source

One option is to add the Unit Test project as “internal to” to your main project, this will allow your Unit Test code to access things marked as “internal”, without letting anyone else do it. Its a simple thing to implement in the AssemblyInfo.cs file:

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

You can use the InternalsVisibleTo property in your AssemblyInfo.cs so that the internal elements are visible to your UnitTest assembly.

+2
source

All Articles