【问题标题】:Moq a Class and Still use its Methods起订量一个类并仍然使用它的方法
【发布时间】:2018-11-12 07:55:17
【问题描述】:

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

    }

【问题讨论】:

    标签: c# asp.net-core nunit moq


    【解决方案1】:

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

    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&lt;T&gt;() 快捷方式直接检索模拟对象:

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

    【讨论】:

    • 嗯,由于某种原因,在记录器和模拟框架之前,它通过单元测试通过运行,现在收到消息:预期: 但是:没有抛出异常”,但是当我调试异常时抛出
    • @JoeThomas 您必须显示一些代码来解释:p 异常在您的 ParseVendorSupply.FromCsv 方法中被捕获,那么您的断言是什么样的?
    猜你喜欢
    • 2014-08-27
    • 2021-02-16
    • 1970-01-01
    • 2017-08-21
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多