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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:32:05+00:00 2026-06-18T22:32:05+00:00

Hi I am trying to learn about the moq framework but I seem to

  • 0

Hi I am trying to learn about the moq framework but I seem to fail to understand a simple concept.Here is the code I am testing:

 public interface ILongRunningLibrary {
    string RunForALongTime(int interval);
}

public class LongRunningLibrary : ILongRunningLibrary {

    public string RunForALongTime(int interval) {
        var timeToWait = interval * 1000;
        Thread.Sleep(timeToWait);

        return string.Format("Waited {0} seconds ", interval);
    }
}

And here are my tests:

private Mock<ILongRunningLibrary> _longRunningLibrary;

    [SetUp]
    public void SetupForTest() {
        _longRunningLibrary = new Mock<ILongRunningLibrary>();
    }

    [Test]
    public void TestLongRunningLibrary() {
        const int interval = 30;
        _longRunningLibrary.Setup(lrl => lrl.RunForALongTime(30))
                          .Returns("This method has been mocked!");
        var result = _longRunningLibrary.Object.RunForALongTime(interval);
        Debug.WriteLine("Return from method was '{0}'", result);
    }

From what I understood from the book I am reading is that this method should return on NUnit console “This method has been mocked”.But in my case the test passes and it returns nothing.

Even if I remove :

_longRunningLibrary.Setup(lrl => lrl.RunForALongTime(30))
                          .Returns("This method has been mocked!");

The test still passes and on the console nothing is printed.

Why is it that “This method has been mocked” is not returned on NUnit console?

  • 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-18T22:32:07+00:00Added an answer on June 18, 2026 at 10:32 pm

    Your first example runs just fine. I think your test runner fails to call Debug.WriteLine. I replaced it with Console.WriteLine and I got the correct output.

    When you removed the Setup call in your second example, the mock will return the default value(which is null) because it uses Loose behavior. When you call Debug.WriteLine with a null parameter it doesn’t throw an exception so your test passes.

    It is a good practice to use StrictBehavior generally.

    _longRunningLibrary = new Mock<ILongRunningLibrary>(MockBehavior.Strict);
    

    Now it will throw an exception because you do not have any setups for RunForALongTime method.

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

Sidebar

Related Questions

I'm trying to learn about this feature of javascript I keep seeing in code,
im trying to learn about web services and i already did a simple webservice
I'm trying to learn about array sorting. It seems pretty straightforward. But on the
I am trying to learn about simple shape detection for a project I'm working
I've been trying to learn about events/delegates, but am confused about the relationship between
I am trying to learn about SQLite databases, but I really hate dealing with
I m trying to learn about index hint, but even after providing hint in
I'm trying to learn about regular expressions but am not doing so well after
I'm trying to learn about using Doctrine 2 by making a very simple script
I am somewhat new to jQuery but have been having fun trying learn about

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.