Hicham Bagui
Hicham Bagui

Reputation: 240

Tests Internal Method raise null reference exception using XUnit

I am trying to setup a return for inner method of object Parameter but the setup of the SetObject throws a nullreference exception so far I am not sure what causing it.
Knowing _parameterManager is mocked object using moq, see the code below:

 _parameterManager.Setup(x => x.Parameters)
    .Returns(new System.Collections.Generic.List<IParameter>()
    {
        new Parameter()
        {
            Description = new ParameterDescriptor()
            {
                ParameterId = new ParameterId()
                {
                    Id = ParametersId.FERTILIZER_SELECTION_ID, ControllerIndex = 0
                },
            }
        },
        new Parameter()
        {
            Description = new ParameterDescriptor()
            {
                ParameterId = new ParameterId()
                {
                    Id = ParametersId.FERTILIZER_1_ID,ControllerIndex = 1
                },
            }   
        },
        new Parameter()
        {
            Description = new ParameterDescriptor()
            {
                ParameterId = new ParameterId()
                {
                    Id = ParametersId.FERTILIZER_2_ID,ControllerIndex = 2
                },
            }
        },   
        new Parameter()
        {
            Description = new ParameterDescriptor()
            {
                ParameterId = new ParameterId()
                {
                    Id = ParametersId.FERTILIZER_3_ID,ControllerIndex = 3
                },
            }
        },
    });

_parameterManager.Setup(x => x.Parameters.FirstOrDefault(y => y.Description.ParameterId.Id == ParametersId.FERTILIZER_SELECTION_ID)
    .SetObject(It.IsNotNull<uint>()))
    .Returns(ParameterResult.Success);

Upvotes: 0

Views: 497

Answers (1)

Hicham Bagui
Hicham Bagui

Reputation: 240

It was actually the Parameter inside the Collection that was a concrete object hence xunit couldn't mock the SetObject method, resulting in raising the mentioned exception.

The answer is either adding Mock parameter objects instead of concrete objects or fill up the parameter object fully.

Upvotes: 1

Related Questions