简体   繁体   中英

Value cannot be null. Parameter name: source at when test mock

Hi I'm using test NUnit with mock in MVC core 2.1 .when test build my test failed when debug test I am getting the below error.

Value cannot be null. Parameter name: source at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

this class test unit mvc core 2.1

 public class Role_Test
        {
            #region Fields   
            private static IRoleService _IRoleService;
            #endregion

            private static Mock<IUnitOfEntity> _iUnitOfEntity;
            private static Mock<IMapper> _mapper;
            private static Mock<ISecurityAuthorizService>  _ISecurityAuthorizService;

            [ClassInitialize]
            public static void ClassInit(TestContext context)
            {
                _iUnitOfEntity = new Mock<IUnitOfEntity>();
                _mapper = new Mock<IMapper>();
                _ISecurityAuthorizService = new Mock<ISecurityAuthorizService>();
                _IRoleService = new RoleService(_iUnitOfEntity.Object,         _mapper.Object, _ISecurityAuthorizService.Object);
            }         
            [TestMethod]
            public void TestMethod()
            {
               var serviceMock = new Mock<IRoleService>();
               //serviceMock.Setup(x => x.LoadRole(0,0));
                var result = _IRoleService.LoadRole(0,0);

                Assert.AreEqual(0, result.Count);
            }
        }
    }



 private readonly IUnitOfEntity _iUnitOfEntity;
        private readonly IMapper _mapper;
        private readonly ISecurityAuthorizService  _ISecurityAuthorizService;
        public RoleService(IUnitOfEntity unitOfEntity, IMapper mapper,  ISecurityAuthorizService ISecurityAuthorizService)
        {
            _iUnitOfEntity = unitOfEntity;
            _mapper = mapper;
            _ISecurityAuthorizService = ISecurityAuthorizService;
        }

       public List<RoleTreeViewModel> LoadRole(long AccessorId  ,decimal AccessorDetailId)
        {
            try
            {
                Dictionary<string, object> outputParam = new Dictionary<string, object>();
                List<SqlParameter> inputParam = new List<SqlParameter>();
                string query = "Sp_Lod_RoleTree_94200";
                inputParam.Add(new SqlParameter("@AccessorId", 2237));
                inputParam.Add(new SqlParameter() { ParameterName = "@AccessorDetailId", SqlValue = 0.0, DbType = DbType.Decimal });
                inputParam.Add(new SqlParameter("@AccessorTypeId", 6));
                //(int)EnumManager.EnumObjectType.Role
                outputParam.Add("@Error", "");
                List<Sp_lod_Group_Result> sp_result = _iUnitOfEntity.ExecSpWithParam<Sp_lod_Group_Result>(query, ref outputParam, inputParam).ToList();
                return _mapper.Map<List<Sp_lod_Group_Result>, List<RoleTreeViewModel>>(sp_result);
            }
            catch (Exception e)
            {
                return new List<RoleTreeViewModel>();
            }
        }

It seems that you've forgotten to set up the your Mock object correctly, so that _iUnitOfEntity.ExecSpWithParam<Sp_lod_Group_Result>(query, ref outputParam, inputParam) retrurns null.

ToList is an extension methods, so

_iUnitOfEntity.ExecSpWithParam<Sp_lod_Group_Result>(query, ref outputParam, inputParam).ToList()

is the same as

Enumerable.ToList(
    _iUnitOfEntity.ExecSpWithParam<Sp_lod_Group_Result>(query, ref outputParam, inputParam)
)

_iUnitOfEntity.ExecSpWithParam returns null. That is the cause of the ArgumentNullException.

I don't know which Mocking Framework you use, but I guess after

_iUnitOfEntity = new Mock<IUnitOfEntity>();

you need to define what ExecSpWithParam<Sp_lod_Group_Result>(query, ref outputParam, inputParam) should return.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM