Skip to content

Instantly share code, notes, and snippets.

@BlythMeister
Created November 24, 2017 11:16
Show Gist options
  • Save BlythMeister/20d81fc6079e65a65361c9d88d67f657 to your computer and use it in GitHub Desktop.
Save BlythMeister/20d81fc6079e65a65361c9d88d67f657 to your computer and use it in GitHub Desktop.
ExpectantStrictMock-Moq
using Moq;
using Moq.Language;
using Moq.Language.Flow;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace MoqHelper
{
public class ExpectantStrictMock<T> : Mock<T> where T : class
{
private readonly List<Action> verifyFunctions;
public ExpectantStrictMock() : base(MockBehavior.Strict)
{
verifyFunctions = new List<Action>();
}
public ISetup<T> SetupExpect(Expression<Action<T>> action, Times times)
{
verifyFunctions.Add(() => Verify(action, times));
return Setup(action);
}
public ISetup<T> SetupExpect(Expression<Action<T>> action, Func<Times> times)
{
return SetupExpect(action, times());
}
public ISetup<T> SetupExpect(Expression<Action<T>> action)
{
return SetupExpect(action, Times.AtLeastOnce);
}
public ISetup<T, T2> SetupExpect<T2>(Expression<Func<T, T2>> action, Times times)
{
verifyFunctions.Add(() => Verify(action, times));
return Setup(action);
}
public ISetup<T, T2> SetupExpect<T2>(Expression<Func<T, T2>> action, Func<Times> times)
{
return SetupExpect(action, times());
}
public ISetup<T, T2> SetupExpect<T2>(Expression<Func<T, T2>> action)
{
return SetupExpect(action, Times.AtLeastOnce);
}
public ISetupSequentialResult<T2> SetupSequenceExpect<T2>(Expression<Func<T, T2>> action, Times times)
{
verifyFunctions.Add(() => Verify(action, times));
return this.SetupSequence(action);
}
public ISetupSequentialResult<T2> SetupSequenceExpect<T2>(Expression<Func<T, T2>> action, Func<Times> times)
{
return SetupSequenceExpect(action, times());
}
public ISetupSequentialResult<T2> SetupSequenceExpect<T2>(Expression<Func<T, T2>> action)
{
return SetupSequenceExpect(action, Times.AtLeastOnce);
}
public void VerifyAllExpectations()
{
foreach (var verifyFunction in verifyFunctions)
{
verifyFunction();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment