Reputation: 3079
I have a wierd problem while trying to unit test my ViewModel classes in UnitTest Project in Visual Studio 2012. I Created a UnitTest Project and added it to my Solutinon. I Added my WPF Project as a reference to my UnitTest project to test the my ViewModel classes and their methods. The Problem is that I can't access my ViewModel classes. Lets say I type :
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
MyWPFProject.ViewModels.MainViewModel
}
}
It's acting like there is no MainViewModel class in ViewModels Folder. What could be the problem here ?
Upvotes: 0
Views: 1038
Reputation: 7804
This is likely due to you declaring your MainViewModel
class with the internal access modifier:
namespace ViewModels
{
internal class MainViewModel
{
...
}
}
The internal access modifier limits visibility to the defining assembly (in this case, your WPF project). If you want to access the class from an external assembly (in this case, your testing project) you can either change the access modifier to public or use the InternalsVisibleTo
attribute.
The InternalsVisibleTo
assembly attribute allows you to specify "friend" assemblies who can view your assemblies internal members. To define the attribute, open AssemblyInfo.cs (should be in your project by default - extend the "Properties" node in the solution explorer) and paste the following besides any of the other assembly attributes:
[assembly: InternalsVisibleTo("Code")]
Change the string "Code" to the name of your unit testing project.
See, in my case I was referencing CodeWPF from Code. Also see the highlighted class AssemblyInfo, it should be pretty self explanatory where to define the attribute.
Upvotes: 2