Sergey Sergey - 1 year ago 47
ASP.NET (C#) Question

Moq SetupSequence doesn't work

I try to use the

method of the Moq 4.5 framework.

Class that should be mocked:

public class OutputManager {
public virtual string WriteMessage(string message) {
// write a message


var outputManagerMock = new Mock<OutputManager>();
var writeMessageCalls = 0;
var currentMessage = String.Empty;

outputManagerMock.Setup(o => o.WriteMessage(It.IsAny<string>())).Callback((string m) => {
message = m;

This code works fine. But I'd like having a different setup for each call of the
method. Well, I use
instead of

var outputManagerMock = new Mock<OutputManager>();
var writeMessageCalls = 0;
var firstMessage = String.Empty;
var secondMessage = String.Empty;

outputManagerMock.SetupSequence(o => o.WriteMessage(It.IsAny<string>()))
.Callback((string m) => {
firstMessage = m;
}).Callback((string m) => {
secondMessage = m;

Then I've got the error:

Error CS0411 The type arguments for method

'SequenceExtensions.SetupSequence<TMock, TResult>(Mock<TMock>,
Expression<Func<TMock, TResult>>)'
cannot be inferred from the usage.

Try specifying the type arguments explicitly.

I've found the possible solution here - SetupSequence in Moq. But it looks like a workaround.

Answer Source

If OutputManager is a dependency for other classes then you should consider abstracting that class to make it easier to mock for your tests.

public interface IOutputManager {
    string WriteMessage(string message);

That would then mean that the implementation of this interface would look like what you had originally with the addition of the interface.

public class OutputManager : IOutputManager {
    public string WriteMessage(string message) {
        // write a message

Given that you originally attempted to mock OutputManager then the assumption here is that it is not the system under test as you typically do not mock the target of the test but rather its dependencies.

So let's assume a dependent class looked something like this.

public class DependentOnIOutputManager {
    private IOutputManager outputManager;

    public DependentOnIOutputManager(IOutputManager outputManager) {
        this.outputManager = outputManager;

    public string SomeMethod(string message) {        
        // write a message
        var output = outputManager.WriteMessage(message);
        //...other code
        return output;

Then an example test could look like this.

public void Moq_SetupSequence_Example() {
    var mock = new Mock<IOutputManager>();

    mock.SetupSequence(x => x.WriteMessage(It.IsAny<string>()))

    var outputManager = mock.Object;

    var sut = new DependentOnIOutputManager(outputManager);

    var first = sut.SomeMethod("1st");

    var second = sut.SomeMethod("2nd");

    Exception e = null;
    try {
    } catch (InvalidOperationException ex) {
        e = ex;