Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
Can I use moq Mock<MyClass> to mock a class, not an interface?

Going through https://github.com/Moq/moq4/wiki/Quickstart, I see it Mock an interface.

I have a class in my legacy code which does not have an interface. When I Mock<MyClass>, I get the following exception:

Additional information: Can not instantiate proxy of class: MyCompnay.Mylegacy.MyClass.

How can I use Moq to mock class from legacy code?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It is possible to Mock concrete classes

[TestClass]
public class PlaceholderParserFixture
{

  public class Foo
  {
     public virtual int GetValue()
     {
        return 11;
     }
  }

  public class Bar
  {
     private readonly Foo _foo;

     public Bar(Foo foo)
     {
        _foo = foo;
     }

     public int GetValue()
     {
        return _foo.GetValue();
     }
  }

  [TestMethod]
  public void MyTestMethod()
  {
     var foo = new Mock<Foo>();
     foo.Setup(mk => mk.GetValue()).Returns(16);
     var bar = new Bar(foo.Object);

     Assert.AreEqual(16, bar.GetValue());
  }

}

but,

  • It must be a public class
  • The method to be mocked must be virtual

The messages I got for:

Making the class internal

Castle.DynamicProxy.Generators.GeneratorException: Type MoqFixture+Foo is not public. Can not create proxy for types that are not accessible.

or, having a non-virtual method

System.NotSupportedException: Invalid setup on a non-virtual (overridable in VB) member: mk => mk.GetValue()

do not match your cannot instantiate message, so something else seems to be wrong.

If you do not have a default constructor on the mocked object you can get that error message

e.g.

public class Foo
{

    private int _value;
    public Foo(int value)
    {
       _value = value;
    }

    public virtual int GetValue()
    {
        return _value;
    }
}

one can get around this by passing values into the Mock<> ctor

e.g.

var foo = new Mock<Foo>(MockBehavior.Strict, new object[] { 11 });
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!