Using Moq to verify calls are made in the correct order

后端 未结 8 1670
青春惊慌失措
青春惊慌失措 2020-12-01 10:00

I need to test the following method:

CreateOutput(IWriter writer)
{
    writer.Write(type);
    writer.Write(id);
    writer.Write(sender);

    // many more         


        
8条回答
  •  遥遥无期
    2020-12-01 10:47

    I've managed to get the behaviour I want, but it requires downloading a 3rd-party library from http://dpwhelan.com/blog/software-development/moq-sequences/

    The sequence can then be tested using the following:

    var mockWriter = new Mock(MockBehavior.Strict);
    using (Sequence.Create())
    {
        mockWriter.Setup(x => x.Write(expectedType)).InSequence();
        mockWriter.Setup(x => x.Write(expectedId)).InSequence();
        mockWriter.Setup(x => x.Write(expectedSender)).InSequence();
    }
    

    I've added this as an answer partly to help document this solution, but I'm still interested in whether something similar could be achieved using Moq 4.0 alone.

    I'm not sure if Moq is still in development, but fixing the problem with the MockSequence, or including the moq-sequences extension in Moq would be good to see.

提交回复
热议问题