值不能为空。参数名称:测试模拟时的来源

时间:2018-07-25 12:05:16

标签: c# asp.net unit-testing asp.net-core mocking

嗨,我在MVC core 2.1中使用带有模拟功能的NUnit测试。 调试测试时出现以下错误。

  

值不能为null。参数名称:来源为   System.Linq.Enumerable.ToList [TSource](IEnumerable`1源)

此类测试单元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>();
            }
        }

1 个答案:

答案 0 :(得分:0)

似乎您忘记了正确设置Mock对象,因此_iUnitOfEntity.ExecSpWithParam<Sp_lod_Group_Result>(query, ref outputParam, inputParam)拒绝使用null。

ToList是扩展方法,所以

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

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

_iUnitOfEntity.ExecSpWithParam返回null。 这就是ArgumentNullException的原因。

我不知道您使用的是哪种Mocking框架,但我想还是

_iUnitOfEntity = new Mock<IUnitOfEntity>();

您需要定义ExecSpWithParam<Sp_lod_Group_Result>(query, ref outputParam, inputParam)应该返回的内容。

希望有帮助。