Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test method call order with Moq

At the moment I have:

    [Test]     public void DrawDrawsAllScreensInTheReverseOrderOfTheStack() {         // Arrange.         var screenMockOne = new Mock<IScreen>();         var screenMockTwo = new Mock<IScreen>();         var screens = new List<IScreen>();         screens.Add(screenMockOne.Object);         screens.Add(screenMockTwo.Object);         var stackOfScreensMock = new Mock<IScreenStack>();         stackOfScreensMock.Setup(s => s.ToArray()).Returns(screens.ToArray());         var screenManager = new ScreenManager(stackOfScreensMock.Object);         // Act.         screenManager.Draw(new Mock<GameTime>().Object);         // Assert.         screenMockOne.Verify(smo => smo.Draw(It.IsAny<GameTime>()), Times.Once(),             "Draw was not called on screen mock one");         screenMockTwo.Verify(smo => smo.Draw(It.IsAny<GameTime>()), Times.Once(),              "Draw was not called on screen mock two");     } 

But the order in which I draw my objects in the production code does not matter. I could do one first, or two it doesn't matter. However it should matter as the draw order is important.

How do you (using Moq) ensure methods are called in a certain order?

Edit

I got rid of that test. The draw method has been removed from my unit tests. I'll just have to manually test it works. The reversing of the order though was taken into a seperate test class where it was tested so it's not all bad.

Thanks for the link about the feature they are looking into. I sure hope it gets added soon, very handy.

like image 726
Finglas Avatar asked Nov 19 '09 19:11

Finglas


People also ask

What is MOQ in testing?

Moq is a mocking framework built to facilitate the testing of components with dependencies. As shown earlier, dealing with dependencies could be cumbersome because it requires the creation of test doubles like fakes. Moq makes the creation of fakes redundant by using dynamically generated types.

How does MOQ mock work?

Mock objects allow you to mimic the behavior of classes and interfaces, letting the code in the test interact with them as if they were real. This isolates the code you're testing, ensuring that it works on its own and that no other code will make the tests fail.


1 Answers

I recently created Moq.Sequences which provides the ability to check ordering in Moq. You may want to read my post that describes the following:

  • Supports method invocations, property setters and getters.
  • Allows you to specify the number of times a specific call should be expected.
  • Provides loops which allow you to group calls into a recurring group.
  • Allows you to specify the the number of times a loop should be expected.
  • Calls that are expected to be called in sequence can be inter-mixed with calls that are expected in any order.
  • Multi-threaded support.

Typical usage looks like:

[Test] public void Should_show_each_post_with_most_recent_first_using_sequences() {     var olderPost = new Post { DateTime = new DateTime(2010, 1, 1) };     var newerPost = new Post { DateTime = new DateTime(2010, 1, 2) };     var posts = new List<Post> { newerPost, olderPost };      var mockView = new Mock<BlogView>();      using (Sequence.Create())     {         mockView.Setup(v => v.ShowPost(newerPost)).InSequence();         mockView.Setup(v => v.ShowPost(olderPost)).InSequence();          new BlogPresenter(mockView.Object).Show(posts);     } } 
like image 56
Declan Whelan Avatar answered Oct 04 '22 10:10

Declan Whelan