首页 > 解决方案 > 模拟函数返回空值,而不是给出模拟值

问题描述

我想在以下功能上编写测试用例

public async Task<List<Author>> GetAuthor()
            {
                using IDbConnection db = GetDbconnection();
    
                var result = await Task.FromResult(dapperWrapper.GetAll<Author>(db, $"SELECT TOP (10) liAuthorId as Id, sFirstName as FirstName, sLastName as LastName from dbo.Authors", null, commandType: CommandType.Text));
                return result;
            }

我试图在 GetAuthor 方法中模拟 GetAll 方法并传递模拟值以返回该函数,但是当我尝试运行测试方法 GetAuthor 时,GetAll 方法给出空值,尽管在返回函数中给出了模拟值。

 public async Task TestMethodTestAsync()
        {

            var expectedConnectionString = "url";
            using IDbConnection db = new SqlConnection(expectedConnectionString);
            var expectedQuery = $"SELECT TOP (10) liAuthorId as Id, sFirstName as FirstName, sLastName as LastName from dbo.Authors";
            List<Author> author = 
                new List<Author> {
                new Author { Id = 1, FirstName = "abc", LastName = "def" },
                new Author { Id = 1, FirstName = "abc", LastName = "def" },
                new Author { Id = 1, FirstName = "abc", LastName = "def" },
                new Author { Id = 1, FirstName = "abc", LastName = "def" },
                new Author { Id = 1, FirstName = "abc", LastName = "def" },
                new Author { Id = 1, FirstName = "abc", LastName = "def" },
                new Author { Id = 1, FirstName = "abc", LastName = "def" },
                new Author { Id = 1, FirstName = "abc", LastName = "def" },
                new Author { Id = 1, FirstName = "abc", LastName = "def" },
                new Author { Id = 1, FirstName = "abc", LastName = "def" }
               };

            mockDapperWrapper.Setup(x => x.GetAll<Author>(db, expectedQuery, null, CommandType.Text)).Returns(author);

            var result = newsRepository.GetAuthor();

作者模型

  public class Author : BaseModel
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

因为我刚开始学习 C# 和新手,如果我做错了,请告诉我。

我想在以下功能上编写测试用例

public async Task<List<NewsArticle>> GetNewsArticle()
    {
        // Create SQL Connection
        using IDbConnection db = GetDbconnection();

        // Create Dapper dynamic parameter set
        DynamicParameters parameter = new DynamicParameters();

        // Output Paramaters
        parameter.Add("@ReturnValue", dbType: DbType.Int32, direction: ParameterDirection.Output);
        parameter.Add("@Message", dbType: DbType.String, direction: ParameterDirection.Output, size: 400);

        var result = await Task.FromResult(dapperWrapper.GetAll<NewsArticle>(db, $"[KafkaPublisher].[GetNewsArticle]", parameter, commandType: CommandType.StoredProcedure));
        
        return result;
    }

我试图在 GetNewsArticle 方法中模拟 GetAll 方法并传递模拟值以返回该函数,但是当我尝试运行测试方法 GetNewsArticle 时,GetAll 方法给出空值,尽管在返回函数中给出了模拟值。

 public async Task Should_Fetch_NewsArticle_Data()
    {

        DynamicParameters parameter = new DynamicParameters();

        // Output Paramaters
        parameter.Add("@ReturnValue", dbType: DbType.Int32, direction: ParameterDirection.Output);
        parameter.Add("@Message", dbType: DbType.String, direction: ParameterDirection.Output, size: 400);
        string expectedQuery = "[KafkaPublisher].[GetNewsArticle]";

        List<NewsArticle> article =
            new List<NewsArticle> {
            new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
            new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
            new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
            new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
            new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
            new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
            new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
            new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
            new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1 },
            new NewsArticle { Timestamp = 1, Version = 1, Headline = "hl", Body = "b1", AuthorId = 1

            };

        mockDapperWrapper.Setup(x => x.GetAll<NewsArticle>(It.IsAny<IDbConnection>(), expectedQuery, null, CommandType.Text)).Returns(article);

        var result = await newsRepository.GetNewsArticle();
        

标签: c#unit-testingasynchronousmoq

解决方案


只有当所有参数都匹配时,Mock 才会返回配置值。传递给模拟设置的db值与在GetAuthor. 用于It.IsAny<IDbConnection>()模拟设置以匹配此类型的任何参数。


推荐阅读