Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8597057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:49:36+00:00 2026-06-12T00:49:36+00:00

I have a test where I pass in an object like so: var repo

  • 0

I have a test where I pass in an object like so:

var repo = new ActualRepo();

var sut = new Sut(repo);

In my test, Repo has one method that I need to actually execute, whilst another method I want to mock out and not execute.

So for example, take this pseudocode:

var repo = new Mock<IRepo>();

repo.Setup(m => m.MethodIWantToCall()).WillBeExecuted();
repo.Setup(m => m.MethodIWantToMock()).Returns(false);

Using Moq, is this possible and how can it be done?

EDIT:
I’ve used TypeMock in the past and you can do something like.

Isolator.When(() => repo.MethodToIgnore()).WillBeIgnored();
Isolator.When(() => repo.MethodToActuallyRun()).WillBeExecuted();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:49:38+00:00Added an answer on June 12, 2026 at 12:49 am

    You can’t do this with Moq if you use the same object unless one of the method is virtual and you are basing your mock on a type rather than an interface.

    That’s because when you are passing a mock object based on an interface, you aren’t passing a real object so it does not have access to the real methods of the object.

    You are passing a dynamic proxy which will respond to methods it has been setup to respond to.

    I believe TypeMock rewrites the assemblies at runtime to achieve this, something Moq definitively doesn’t do.

    If you want to achieve similar results with Moq:

    • You could mock both methods
    • You would have to extract both methods to different dependencies so as to mock one dependency and not the other.
    • You could have the method you need mocked be virtual, which would be the solution I would prefer.

    EDIT : I edited my answer for correctness after reading AlanT’s answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to object-oriented JavaScript. Currently, I have an object that I need
I have codes just like follows: class test: def do_something(): pass test1 = test()
I have the following test: [Test] public void VerifyThat_WhenInitializingTheLoggingInterceptionFacility_TheLoggingInterceptorIsAdded() { var kernel = new
I have a object like: var my_component = {}; Later I assign some properties
I have a Test Class, that test the access of all page with different
I have a test sample about coherence lock-unlock mechanism like this: public class Test
I have this test [Test] public void SaveInventoryItemLoad_Will_Call_WCF_Service_SaveInventoryItemLoad() { adapter.SaveInventoryItemLoad(new List<InventoryItemLoadProxy>()); itemMasterBusinessClientMock.Verify(x => x.SaveInventoryItemLoad(It.IsAny<List<InventoryItemLoadProxy>>()),
I have a test project, with an App.config that sets up default values for
I have a test where I'm mocking 2 service calls that get called in
I have a javascript object which has some defined variables and attaches some event

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.