我正在使用Moq框架对一个类进行模拟。然而,我无法调用该类的方法。如何解决下面的单元测试中出现的问题?尝试编译程序以在类中提取Moq中的方法。以下是错误信息:
类:
using System;
using ElectronicsStore.Models;
using Microsoft.Extensions.Logging;
namespace ElectronicsStore.Service
{
public class ParseVendorSupply
{
private readonly ILogger _logger;
public ParseVendorSupply(ILogger logger)
{
_logger = logger;
}
public VendorSupply FromCsv(string csvLine)
{
VendorSupply vendorsupply = new VendorSupply();
try
{
string[] values = csvLine.Split(',');
if (values.Length > 3)
{
throw new System.ArgumentException("Too much data");
}
vendorsupply.VendorId = Convert.ToInt16(values[0]);
vendorsupply.ProductId = Convert.ToInt16(values[1]);
vendorsupply.Quantity = Convert.ToInt16(values[2]);
}
catch (Exception)
{
_logger.LogInformation("An exception was thrown attempting");
}
return vendorsupply;
}
}
}
public Startup(IConfiguration configuration, ILogger<Startup> logger)
{
Configuration = configuration;
_logger = logger;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(new LoggerFactory().AddConsole().AddDebug());
services.AddLogging();
NUnit测试:
public class ParseVendorSupplyNunit
{
[Test]
public void FromCsv_ParseCorrectly_Extradata()
{
var logger = new Mock<ILogger>();
Mock<ParseVendorSupply> parseVendorSupplytest = new Mock<ParseVendorSupply>(logger);
var test = new Mock<ParseVendorSupply>(logger);
string csvLineTest = "5,8,3,9,5";
parseVendorSupplytest.FromCsv
// Receive error: Mock<ParseVendorSupply>' does not contain a definition for 'FromCsv' and no accessible extension method 'FromCsv' accepting a first argument of type 'Mock<ParseVendorSupply>' could be found (are you missing a using directive or an assembly reference?)
}
ParseVendorSupply.FromCsv
方法中,那么你的断言看起来是什么样子的? - Kevin Gosse