MustBeCalled

2 posts, 0 answers
  1. Edoardo
    Edoardo avatar
    5 posts
    Member since:
    Mar 2011

    Posted 16 May 2012 Link to this post

    I have to verify multiple calls with different arguments to a method and MustBeCalled does the job.
    Some pieces are missing, though.

    For example, let's say I want to verify that the "Prompt(string text)" method is called just twice with the following two texts only: "hello", "world".

    Mock.Arrange(() => foo.Prompt("hello")).DoNothing().MustBeCalled();
    Mock.Arrange(() => foo.Prompt("world")).DoNothing().MustBeCalled(); 

    How do I know that it wasn't called a third time with any other argument?

    Thanks!

    Also, I wanted to add that it's very annoying and especially time consuming not having any information about which MustBeCalled expectation failed.
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 21 May 2012 Link to this post

    Hi Edoardo,
    Thanks again for contacting us.

    The idea behind MusBeCalled is asserting something that is already invoked. There can be numerous possibilities how a particular method can be called but the task of the assertion is to make sure that all your expectations are executed as you have set / arranged them.

    However, you can write the test in the following way that should make sure that for any argument other than the ones for which the expectations are set will fail the test.
    var foo = Mock.Create<Foo>();
     
    Mock.Arrange(() => foo.Prompt("hello")).DoNothing().InSequence().MustBeCalled();
    Mock.Arrange(() => foo.Prompt("world")).DoNothing().InSequence().MustBeCalled();
    Mock.Arrange(() => foo.Prompt(Arg.AnyString)).DoNothing().InSequence().OccursNever();
     
    foo.Prompt("hello");
    foo.Prompt("world");
    foo.Prompt("foo");
     
    Mock.Assert(foo);


    Secondly, you are right about the exception that is been thrown for MustBeCalled, it should be more verbose on what call it failed for. I am including a task for it and hopefully it will be resolved in the next build and thanks for pointing that out.

    You can further the follow the task here:
    http://www.telerik.com/support/pits.aspx#/public/justmock/11174

    Kind Regards
    Ricky
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft R3 2016 release webinar banner
Back to Top