模拟函数返回空值,而不是给出模拟值



我想为以下函数编写测试用例

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();

Mock只有在所有参数匹配时才会返回配置值。传入模拟设置的db值与在GetAuthor中创建的值不同。使用It.IsAny<IDbConnection>()进行模拟设置,以匹配此类型的任何参数。

最新更新