Why is Mock.Arrange().DoNothing() not available?

5 posts, 1 answers
  1. cori
    cori avatar
    14 posts
    Member since:
    Jul 2010

    Posted 12 Aug 2010 Link to this post

    I'm trying to mock the static System.IO namespace methods in C#, so I have calls like this:

                Mock.SetupStatic<File>();
                Mock.Arrange( () => File.AppendText(Arg.AnyString)).DoNothing();

    because I want to stop the filesystem from writing anything.  When I do so, however, I receive the following error:
    'Telerik.JustMock.Expectations.FuncExpectation<System.IO.StreamWriter>' does not contain a definition for 'DoNothing' and no extension method 'DoNothing' accepting a first argument of type 'Telerik.JustMock.Expectations.FuncExpectation<System.IO.StreamWriter>' could be found (are you missing a using directive or an assembly reference?)

    In a different test method in the same class using a static class of my own, I am able to make that call without any difficulty.

    What am I missing?

  2. Answer
    Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 13 Aug 2010 Link to this post

    Hello Cori,

    Thanks for reporting the issue. I tried to mock File.AppendText the following way that works nicely:

    [TestMethod]
    public void ShouldStopFile_AppendTextForWriting()
    {
        Mock.Arrange(() => File.AppendText(Arg.AnyString));
        File.AppendText("anything");
    }


    Therefore, I found out that DoNothing modifier is not available for members that return a value. But this an optional modifier added to increase readability and without it things should work just fine. But, yes that goes into our product backlog and should be fixed for the next build.

    Also, please make sure that you have MockClassAttribute on top of your class which is required for mocking framework members.

    Hope this information helps.

    Regards,
    Mehfuz

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft R3 2016 release webinar banner
  4. cori
    cori avatar
    14 posts
    Member since:
    Jul 2010

    Posted 13 Aug 2010 Link to this post

    So is DoNothing() essentially just decoration - in other words, is that the default behavior of Mock.Arrange()?

    I suppose that makes sense - if you're mocking a method and you don't tell the mock to do something then be default it will do nothing. Is that the way I should be interpreting this?
  5. Chris
    Admin
    Chris avatar
    221 posts

    Posted 13 Aug 2010 Link to this post

    Hello Cori,

    Yes, exactly, DoNothing is just a decoration method which does nothing but improves readability. Omitting it won't change anything.

    Kind regards,
    Chris
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. cori
    cori avatar
    14 posts
    Member since:
    Jul 2010

    Posted 13 Aug 2010 Link to this post

    Perfect, thanks!
Back to Top
DevCraft R3 2016 release webinar banner