UnoSD / Moq.Dapper

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

When writing a test for query which has .Any method it throws an error Value cannot be null #71

Closed ankur4822 closed 3 years ago

ankur4822 commented 3 years ago

There are a couple of queries in my repository that use Any() for dapper. While I was writing the unit test for it they all fail with the same error Value cannot be null.

Sample code

This is the method which I want to test

public IEnumerable<SiteRemotePrefixResponse> CheckIfPrefixExistsForSiteIds(string siteIds, DateTimeOffset startDateTime, DateTimeOffset endDateTime)
        {
            var query = string.Empty;
            var responses = Enumerable.Empty<SiteRemotePrefixResponse>();

            using (var dbConnection = Connection.CreateDbConnection())
            {
                dbConnection.Open();

                query = $@"SELECT siteId,
                           CASE COUNT(prefix) WHEN 0 THEN 0 ELSE 1 END AS isAvailable
                           FROM ABC
                           WHERE site_id = ANY(@SiteId)
                           AND bucket_start BETWEEN '{startDateTime.DateTime:yyyy-MM-dd HH:mm:ss}' AND '{endDateTime.DateTime:yyyy-MM-dd HH:mm:ss}'
                           GROUP BY site_id
                           ORDER BY 2 DESC
                        ";

                var siteArray = siteIds.Split(',');
                var parameters = new { SiteId = siteArray };

                responses = dbConnection.Query<SiteRemotePrefixResponse>(query, parameters, commandTimeout: 600);
            }

            return responses;
        }

Unit test for this method


[Fact]
        public void CheckIfRemotePrefixExistsForSiteIds_OkResult()
        {
            //ARRANGE
            var siteIds = "'SIT-1234','SIT-3456'";
            var startDateTime = DateTime.Now.AddDays(-1);
            var endDateTime = DateTime.Now;
            var siteRemotePrefixResponses = MockedEntitiesRepository.mockedSiteRemotePrefixResponses;
            var siteArray = siteIds.Split(',');
            var parameters = new { SiteId = siteArray };
            mockedDbConnection.SetupDapper(x => x.Query<SiteRemotePrefixResponse>(It.IsAny<string>(), parameters, null, true, 600, null)).Returns(siteRemotePrefixResponses);

            //ACT
            var result = repository.CheckIfRemotePrefixExistsForSiteIds(siteIds, startDateTime, endDateTime);

            //ASSERT
            Assert.Equal(JsonConvert.SerializeObject(siteRemotePrefixResponses), JsonConvert.SerializeObject(result));
        }

This is the stacktrace

   at System.Text.RegularExpressions.Regex.Replace(String input, MatchEvaluator evaluator)
   at System.Text.RegularExpressions.Regex.Replace(String input, String pattern, MatchEvaluator evaluator, RegexOptions options)
   at Dapper.SqlMapper.PackListParameters(IDbCommand command, String namePrefix, Object value) in C:\projects\dapper\Dapper\SqlMapper.cs:line 2086
   at Dapper.CommandDefinition.SetupCommand(IDbConnection cnn, Action`2 paramReader) in C:\projects\dapper\Dapper\CommandDefinition.cs:line 129
   at Dapper.SqlMapper.<QueryImpl>d__138`1.MoveNext() in C:\projects\dapper\Dapper\SqlMapper.cs:line 1078
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 723
   at XCASystemAPI.Core.Repository.CheckIfRemotePrefixExistsForSiteIds(String siteIds, DateTimeOffset startDateTime, DateTimeOffset endDateTime) in Repository.cs:line 366
   at SystemAPI.Tests.API.RepositoryTest.CheckIfRemotePrefixExistsForSiteIds_OkResult() in RepositoryTest.cs:line 149

Kindly suggest what could be done to solve this error.