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 298355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:43:26+00:00 2026-05-12T06:43:26+00:00

my team has made the decision recently to use Moq as our mocking framework

  • 0

my team has made the decision recently to use Moq as our mocking framework for its tremendous flexibility and highly readable syntax. As we’re new to it, I’m stumbling on what appears to be simple questions–searches (here, Google, etc.) find plenty of discussions on other nuances of Moq, but not necessarily what I’m after, and the few seemingly related questions have turned into red herrings.

We’re testing a class that has an external dependency (Amazon SimpleDb to be precise) but don’t want our tests bound to having a live connection out. A particular method:

  • Applies some “business” logic
  • If appropriate, invokes a call out to SDB via a provider we’ve built, let’s call it SaveItem()

I want to unit test this such that we setup the context required and insure that SaveItem() was invoked, but in a manner that SaveItem() really isn’t invoked (because A) the provider to SDB is a mock that isn’t fully hydrated and will likely bomb and B) I don’t want to have to pay for that transaction hundreds and thousands of times).

When dealing with methods that returned a value, this was trivial.

mockDb.Setup(d => d.GiveMeSomething()).Returns("Foo");

In the case that I outline above though, my “SaveItem()” method is void and therefore the option to use Moq’s Returns() method isn’t available. And while I can setup a callback to verify SaveItem() is invoked, I can’t however seem to get it to not actually do anything.

Naive/hopeful, I thought the following would work, but it appears to still invoke the method:

mockDb.Setup(d => d.SaveItem(It.IsAny<object>()));

So the million dollar question: What’s the Moq of the following fictitious code?

mockDb.Setup(d => d.SaveItem(It.IsAny<object>())).STOP_RIGHT_HERE();
  • 1 1 Answer
  • 1 View
  • 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-05-12T06:43:26+00:00Added an answer on May 12, 2026 at 6:43 am

    If the SaveItem() method is virtual or abstract, and you’re not setting Callbase = true, then the method should be re-implemented to do nothing by the mock.

    You should be able to do:

    mockDb.Setup(d => d.SaveItem(It.IsAny<object>())).Verifiable();
    
    ...  test here ...
    
    mockDb.Verify();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our team has recently been working on a logic and data layer for our
Our team has recently been considering pushing out a minor registry fix to users
My capstone team has decided to use Qooxdoo as the front end for our
Our team has just started developing for the Sitecore CMS. We find that after
One of my team has recently come across an interesting glitch in the matrix.
I work with a team that has a sharepoint site currently runnning and its
My organization has made an upper level decision to move to maven as the
I make use of a great class made by Jaisen: http://github.com/jmathai/twitter-async/tree/master . Recently twitter
Thank you in advance for your assistance. Our team has been spoiled by MS
One of our team member (located in different region) has shelved changes in P4

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.