c# - Moq 一个类并仍然使用它的方法

标签 c# asp.net-core nunit moq

我正在用 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?)

    }

最佳答案

Moq 通过 .Object 公开模拟对象属性(property)。所以在你的情况下,你可以这样做:

parseVendorSupplytest.Object.FromCsv(csvLineTest);

就是说。我不确定这是你首先想要做的。假设您正在尝试测试 ParseVendorSupply使用模拟记录器,我相信您的代码应该如下所示:

[Test]
public void FromCsv_ParseCorrectly_Extradata()
{
    var logger = new Mock<ILogger>();
    var parseVendorSupply = new ParseVendorSupply(logger.Object);

    string csvLineTest = "5,8,3,9,5";

    var result = parseVendorSupplytest.FromCsv(csvLineTest);

   // Add your assertions here 
}

另请注意,您可以使用 Mock.Of<T>()如果不需要任何设置,直接检索模拟对象的快捷方式:

var parseVendorSupply = new ParseVendorSupply(Mock.Of<ILogger>());

关于c# - Moq 一个类并仍然使用它的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53257882/

相关文章:

c# - 为什么 linq-2-sql 会创建额外的不必要的对象?

c# - Javascript 和必填字段验证器

c# - 如何更新远程 ms sql server 上的数据库(EF 代码优先)

asp.net-core - 如何从 ASP.NET Core 中的 Controller 操作提供 blazor 应用程序

asp.net-core - 包控制台管理器内的 "dnvm"命令不显示任何内容

asp.net-core - 暴露应用程序命令的强类型 ID?

visual-studio - nUnit 测试适配器 10 秒限制 : long-running tests in nUnit

c# - 无法发现 NUnit 测试用例

c# - 如何设置gridview边框的样式?

java - Junit实例创建分别通过方法或类