7

C # & ldquo; Undefined method & rdquo; When adding a new method to a sim...

 2 years ago
source link: https://www.codesd.com/item/c-undefined-method-when-adding-a-new-method-to-a-simulated-interface.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

C # & ldquo; Undefined method & rdquo; When adding a new method to a simulated interface

advertisements

I am currently writing unit tests that uses methods from one of my mocked interfaces. Everything works fine, but when I add a new method to the interface (the connection between the interface and its base class is fine), the mocked interface in my unit test project does not pick up the new method, throwing a "method not defined" error. Here is a basic example of how I am adding new methods.

Base Class:

namespace MyProject.Business
{
    public class MyProjectValidator: IValidator
    {
        private readonly IValidator mValidator;

        public MyProject(IValidator validator)
        {
            this.mValidator= validator;
        }

        public bool myMethod()
        {
            return true;
        }

        public bool myNewMethod()
        {
            return true;
        }
    }
}

Interface

namespace MyProject.Business
{
    public interface IValidator
    {
       bool myMethod();
       bool myNewMethod ();
    }
}

Unit Tests (different project, same solution)

using MyProject.Business;
namespace MyProject.Tests.Business
{
    [TestClass]
    public class MyProjectTests
    {

        [TestMethod]
        public void testMethods()
        {
             var testValidator= new Mock<IValidator>();
             var testMyProject= new MyProject(testValidator.Object);

             var testOld = testValidator.myMethod(); // testOld = true
             var testNew = testValidator.myNewMethod(); // error: myNewMethod() not defined
         }
     }
}

As I commented above, my newly added method is not being recognized. My old method was created before the unit test project was created (I assume, I am inheriting code from a former developer). I tried checking the references in the unit test project for a failed linkage but all of the functions of IValidator appear. I have tried rebuilding and restarting & rebuilding both the solution and the project to no avail.

Any guidance concerning why "myNewMethod()" is not being recognized in my unit test project would be appreciated.

Update: added the using line as requested.


Going off of a comment by @31eee384, the problem was indeed related to the lack of an object reference. Changing the call to testValidator.Object.myNewMethod(); (the object reference to the interface for the original method had been hidden elsewhere) allowed the method to be recognized. Thank you all for the input!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK