18

How do I creat a pure stub using Moq? With Rhino Mocks I did it like this:

[TestFixture]
public class UrlHelperAssetExtensionsTests
{
     private HttpContextBase httpContextBaseStub;
     private RequestContext requestContext;
     private UrlHelper urlHelper;
     private string stylesheetPath = "/Assets/Stylesheets/{0}";

     [SetUp]
     public void SetUp()
     {
          httpContextBaseStub = MockRepository.GenerateStub<HttpContextBase>();
          requestContext = new RequestContext(httpContextBaseStub, new RouteData());
          urlHelper = new UrlHelper(requestContext);
     }

     [Test]
    public void PbeStylesheet_should_return_correct_path_of_stylesheet()
    {
        // Arrange
        string expected = stylesheetPath.FormatWith("stylesheet.css");

        // Act
        string actual = urlHelper.PbeStylesheet();

        // Assert
        Assert.AreEqual(expected, actual);
    }
}

How would I create a stub for MockRepository.GenerateStub<HttpContextBase>(); using Moq? Or should I just stay with Rhino Mocks?

Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234

3 Answers3

17

Here is my suggestion for you:

Mock<HttpContextBase> mock = new Mock<HttpContextBase>();
mock.SetupAllProperties();

Then you have to do the setup.

For further informations see homepage of the MOQ project.

tronda
  • 3,902
  • 4
  • 33
  • 55
Fischermaen
  • 12,238
  • 2
  • 39
  • 56
  • What setup do I require? I just need to use it like in my code. I don't use httpContextBaseStub any place else. – Brendan Vogt Nov 23 '11 at 12:37
  • You have to setup it in that way, your class under test needs it. It depends on the unit test you want to run. You can say it in general. – Fischermaen Nov 23 '11 at 12:40
  • 1
    Unfortunately your sample gives me no information what your class under test (UrlHelper) needs. Mocking is no "blackbox feature", you configure the mock in a way, so your class under test has to handle with a special situation, or something like that. – Fischermaen Nov 23 '11 at 12:49
  • I'm testing a helper method, this is why I have it like urlHelper.PbeStylesheet(). Then how would you write the test then to test my helper method? – Brendan Vogt Nov 23 '11 at 12:53
  • 1
    @BrendanVogt: You have to analyse the helper method. Then you will see a couple of unit test to be written. (For the normal case and for special cases). For each of those unit tests you will need a special setup for your mock. But that depends on the code of the helper class you are testing. I can't give you a setup for all purposes! – Fischermaen Nov 23 '11 at 12:57
9

A bit late to the party here but there's still not a sufficient answer here in my opinion.

Moq doesn't have explicit stub and mock generation in the same way RhinoMocks does. Instead, all setup calls, e.g. mockObject.Setup(x => blah ...) create a stub.

However, if you want the same code be treated as a mock, you need to call mockObject.Verify(x => blah ...) to assert that the setup ran as you expected.

If you call mockObject.VerifyAll(), it will treat everything you have setup as mocks and this is unlikely to be the behaviour you wish, i.e. all stubs treated as mocks.

Instead, when setting up the mock use the mockObject.Setup(x => blah ...).Verifiable() method to mark the setup explicitly as a mock. Then call mockObject.Verify() - this then only asserts the setups that have been marked with Verifiable().

Digbyswift
  • 10,310
  • 4
  • 38
  • 66
  • Is that still true? I see on their page the use of .Returns(). Oh returnt is to impose a behaviour, ok. Probably worth mentioning that though :-) – toughQuestions Aug 13 '20 at 14:32
  • @toughQuestions Not quite sure what you're getting at here, could you clarify your comment? Moq's `Returns()` is irrelevant as it could be used to generate the behaviour of a stub or mock, althoug a purest might disagree. For what it's worth, I currently use NSubstitue. – Digbyswift Aug 14 '20 at 11:21
0
var mockHttpContext = new Mock<HttpContextBase>();
Myles McDonnell
  • 12,943
  • 17
  • 66
  • 116
  • 1
    I know there is a difference in a stub and mock, but does your implementation create a mock or stub? Looks like a mock to me? – Brendan Vogt Apr 06 '12 at 19:34
  • 5
    The naming refers to the way you use this object. So, if you won't verify anything on this objects it's a stub, if you will - it's a mock. – BartoszKP Aug 12 '13 at 16:59