Moq a Class and Still use its Methods

Viewed 343

I am doing a Mock of a class, with Moq framework. However, I am not able to grab or call the Class's methods. How would I resolve this issue in the unit test below? Trying to compile program to extract the methods in Moq in class. Error is listed below.

Class:

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 Test:

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?)

    }
1 Answers

Moq exposes the mocked object through the .Object property. So in your case, you could do:

parseVendorSupplytest.Object.FromCsv(csvLineTest);

That said. I'm not sure this is what you wanted to do in first place. Assuming you're trying to test ParseVendorSupply using a mocked logger, I believe your code should look like:

[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 
}

Also note that you can use the Mock.Of<T>() shortcut to directly retrieve the mocked object if you don't need any setup:

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