UnoSD / Moq.Dapper

Moq extensions for Dapper methods.
GNU General Public License v2.0
173 stars 78 forks source link

Strange little null bug for Nullable enum types in complex types. #66

Open KnightSwordAG opened 4 years ago

KnightSwordAG commented 4 years ago

So I have an object like this:

public class CptExtension
    {
        public int Id { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
        public bool Active { get; set; }
        public DateTime EffectiveDate { get; set; }
        public DateTime? RetireDate { get; set; }
        public CodeClassification? CodeClassification { get; set; }
        public IList<EdsModifier> DisallowedModifiers { get; set; } = new List<EdsModifier>();
        public Subcategory? SubCategory { get; set; }
        public ContrastSetting? ContrastSetting { get; set; }
        public BusinessUsage? BusinessUsage { get; set; }
        public FamilyIndicator? FamilyIndicator { get; set; }
        public MultiProcCode? MultiProcCode { get; set; }
    }

I have setup my mapping with this:

        public void SetupDapperCalls(int? id = null, string code = null, object model = null)
        {
            if (id.HasValue)
            {
                DbConnection.SetupDapperAsync(c => c.QueryAsync<CptExtension>(It.IsAny<string>(), null, null, null, CommandType.StoredProcedure))
                    .ReturnsAsync(CptExtensions.Where(x => x.Id == id.Value));
                return;
            }

            if (!string.IsNullOrWhiteSpace(code))
            {
                DbConnection.SetupDapperAsync(c =>
                        c.QueryAsync<CptExtension>(It.IsAny<string>(), null, null, null, CommandType.StoredProcedure))
                    .ReturnsAsync(CptExtensions.Where(x => x.Code.Equals(code, StringComparison.OrdinalIgnoreCase)));
                return;
            }

            if (model != null)
            {
                return;
            }

            DbConnection.SetupDapperAsync(c =>
                    c.QueryAsync<CptExtension>(It.IsAny<string>(), null, null, null, CommandType.StoredProcedure))
                .ReturnsAsync(CptExtensions);
        }

When the test call is made, the nullable enum types all return null. So my subsequent query integrity checks all fail, since the data is corrupted in transit. Now I know this is the fault of the mapping, but it shouldn't be there at all, my expected values should be unaltered as a result of the mock mapping. So, any ideas?