0

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?

Sumurai8
  • 20,333
  • 11
  • 66
  • 100
gt.guybrush
  • 1,320
  • 3
  • 19
  • 48
  • When you declared at least one constructor in your class, the default constructor no longer is being generated. So in both of your classes the private constructor is pointless. – Daniel Hilgarth Sep 20 '13 at 13:13
  • Thanks, i simply find the private one in the post where i learn fluent constructor and use it, 1 less row to write :) – gt.guybrush Sep 20 '13 at 13:47

1 Answers1

0

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

ilmatte
  • 1,732
  • 16
  • 12
  • yes, can be a workaround, i read it but didn't think it because iam searching to isolate from constructor completely and write .WithDependency(fake) – gt.guybrush Sep 20 '13 at 14:43
  • In that case you need to create an interface: IDependenceClass and modify the signature of WithDependency to accept the interface. Then you can provide a fake implementation or use a mocking framework. – ilmatte Sep 20 '13 at 14:54
  • I feared some like this... it's simply oversized for this kind of test need – gt.guybrush Sep 20 '13 at 14:57
  • test now but don't work: my test methof is in folder "Tests" of a project named "FunzIA.UnitTest.DL" i add to assemblyInfo this: [assembly: InternalsVisibleTo("FunzIA.UnitTest.DL")] but compiler say that no method found whit no parameter – gt.guybrush Sep 30 '13 at 14:56
  • The syntax is correct. The attribute should be placed in the assembly in which MyClass is defined. Did you check it's placed in the right assembly? – ilmatte Oct 01 '13 at 07:45
  • yes MyClass is a domain class and i put [assembly: InternalsVisibleTo("FunzIA.UnitTest.DL")] in DomainlLayer project – gt.guybrush Oct 09 '13 at 09:40
  • http://cubeupload.com/im/dfMn3o.jpg http://cubeupload.com/im/LYAL3Z.jpg http://cubeupload.com/im/7RDsce.jpg – gt.guybrush Oct 09 '13 at 10:21
  • Solved :) Assembly name of the project was different from solution name (idea of testing it come from this post: http://stackoverflow.com/questions/18845634/internalsvisibleto-does-not-work) – gt.guybrush Oct 09 '13 at 11:02