Docking and testing classes with several constructor parameters

My service level classes are configured to install the constructor. When these classes are taunted and tested, how can I avoid passing in nullfor parameters that I will not use in my test?

Example code that I am trying to avoid:

[TestFixture]
public class SomeServicesTests
{
    SomeServices _someServices;

    [SetUp]
    public void Initialize()
    {
        _someServices= new SomeServices (null, 
                                        new StubRepositoryOne(), 
                                        null, 
                                        null, 
                                        new StubRepositoryTwo(), 
                                        null, 
                                        null, 
                                        null,
                                        null);

    }

    [Test]
    public void Test_Something()
    {
        string input = "yes";
        string exptectedOutput = "no";

        string output = _someServices.SomeFunction(input); // uses StubRepositoryOne and StubRepositoryTwo
        Assert.AreEqual(exptectedOutput, output);
    }
}
+3
source share
3 answers

I usually check the entered parameters against zero. I usually just walked

new Mock<T>().Object

for them.

+2
source

Adding an answer after accepting it, but ...

, , . , - - , , , , , , .

, , , , . :

internal SomeServices(IServiceOne one, IServiceTwo two)
{
}

/ . , , :

public IServiceOne One
{
   get { return _one; }
   internal set { _one = value; }
}

, InternalsVisibleTo , .

+3

Commit all missing arguments and always check for null arguments inside SomeService. Checking the inputs for zero every time you use it is cumbersome. So, it's best to always check for null in the constructor and throw an ArgumentNullException.

You note that some input arguments are private. They must be removed for your injection sample to work.

+2
source

Source: https://habr.com/ru/post/1793509/


All Articles