Question

I have one class that have a property of another class type:

public class MyClass
{
    public DepdendenceClass DependenceProp { get; set; }

    private MyClass(){}
    public MyClass(MyClassFactory factory)
    {
        this.DependenceProp = factory.DependenceProp;
    }
}

public class DepdendenceClass
{
    public int key { get; set; }

    private DepdendenceClass() { }
    public DepdendenceClass(DepdendenceClassFactory factory)
    {
        this.key = factory.key;
    }
}

I hide the base constructor since I do not want that can be create empty instances

Each have a fluent ordered constructor that let me write this test:

[Test]
public void MyClassConstructorTest()
{
    DepdendenceClass depdendencePropExpected = DepdendenceClassFactory.InitCreation()
                                                    .WithKey(1)
                                                    .Create();

    MyClass myClassActual = MyClassFactory.InitCreation()
                        .WithDependency(depdendencePropExpected)
                        .Create();
    Assert.That(myClassActual.DependenceProp, Is.EqualsTo(depdendencePropExpected));
}

Is there a way to isolate the MyClassConstructor Test from the constructor of the second class?

Was it helpful?

Solution

I hide the base constructor since I do not want that can be create empty instances

If you want to hide the default constructor to the rest of the application but you want to use it in the test project you should define explicitly the default constructor as internal:

internal MyClass(){}

and then add the InternalsVisibleTo attribute to the AssemblyInfo.cs of the project in which MyClass is defined.

If your test project's name is: MySolution.Fixtures you will have:

[assembly: InternalsVisibleTo("MySolution.Fixtures")]

In this way you can use the default constructor in your test and isolate it from the fluent constructor

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top