Using Moq testing overloaded methods, get System.MissingMethodException: "Method Not Found"

1

I have this interface:

public interface IProductsRepository
{

    // Use this method for non transactional commits, and return the new primary key.
    Task<int> CreateNewProductItem(ProductExt product);

    // use this method for transactional commits, and return the new primary key
    Task<int> CreateNewProductItem(ProductExt product, MySqlConnection conn, MySqlTransaction transaction);

Now I'm trying to write some Moq setups:

            Mock<IProductsRepository> _ProductRepositoryMoq = new Mock<IProductsRepository>();

            _ProductRepositoryMoq.Setup(r => r.CreateNewProductItem(It.IsAny<ProductExt>()))
                .ReturnsAsync((ProductExt p) =>
                {
                    p.ID = productList.Last().ID + 1;
                    return p.ID;
                });
            _ProductRepositoryMoq.Setup(r => r.CreateNewProductItem(It.IsAny<ProductExt>(), It.IsAny<MySqlConnection>(), It.IsAny<MySqlTransaction>()))
                .ReturnsAsync((ProductExt p, MySqlConnection c, MySqlTransaction t) =>
                {
                    p.ID = productList.Last().ID + 1;
                    return p.ID;
                });

When I try to run the unit test and before it happens, I get this error:

System.MissingMethodException
  HResult=0x80131513
  Message=Method not found: 'System.Threading.Tasks.Task`1<Int32> 
  Repositories.ProductManager.IProductsRepository.CreateNewProductItem(ProductManagerModels.ProductExt, MySql.Data.MySqlClient.MySqlConnection, MySql.Data.MySqlClient.MySqlTransaction)'.

I don't get it. This method is defined in the usual way and it's clearly visible, so why is Moq throwing a fit that it cannot find CreateNewProductItem(product, MySqlConnection, MySqlTransaction) ??

NOTE: I've already tried nuking all bin/obj in all projects to force VS 2017 to rebuild the entire solution, and although unlikely, I did see if there was a remote possibility that is was in the GAC, but there was not one item from my projects that has been placed into the GAC. Therefore it has be elsewhere.

SECOND NOTE: As I stated, this is happening before I run any unit tests in the [TestInitialize] method. I'm using MSTest for unit testing. Now the funny thing is that I wrote up a spike to use the database directly instead of the Moq, and NO system.MethodMissingException is thrown. I also verified that the data has indeed been inserted and/or updated in the MySQL Database.

c#
unit-testing
moq
asked on Stack Overflow Dec 15, 2017 by Steve Brother • edited Dec 17, 2017 by Steve Brother

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0