This is a migrated thread and some comments may be shown as answers.

MustBeCalled

1 Answer 77 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Edoardo
Top achievements
Rank 1
Edoardo asked on 16 May 2012, 04:01 PM
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.

1 Answer, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 21 May 2012, 04:17 PM
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 >>

Tags
General Discussions
Asked by
Edoardo
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Share this question
or