This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq.Expressions; | |
using Moq; | |
using Xunit; | |
namespace DynamicVerify | |
{ | |
public class ExpectedCallFromMemberData | |
{ | |
[Theory] | |
[MemberData(nameof(GetMemberData))] | |
public void Bar_calls_right_method_on_IFoo(int number, Expression<Action<IFoo>> expectedCall, Times times, string message) | |
{ | |
var fooMock = new Mock<IFoo>(); | |
var sut = new Bar(fooMock.Object); | |
sut.DoBar(number); | |
fooMock.Verify(expectedCall, times, message); | |
} | |
private static IEnumerable<object[]> GetMemberData() | |
{ | |
Func<Expression<Action<IFoo>>, Expression<Action<IFoo>>> fooCall = action => action; | |
return new List<object[]> { | |
new object[] {1, fooCall(foo => foo.DoFoo1()), Times.Once(), "Expected call on DoFoo1." }, | |
new object[] {2, fooCall(foo => foo.DoFoo2()), Times.Once(), "Expected call on DoFoo2." }, | |
new object[] {3, fooCall(foo => foo.DoFoo1()), Times.Never(), "Argument 3 should not call IFoo at all." }, | |
}; | |
} | |
} | |
public interface IFoo | |
{ | |
void DoFoo1(); | |
void DoFoo2(); | |
} | |
public class Bar | |
{ | |
private IFoo _foo; | |
public Bar(IFoo foo) | |
{ | |
_foo = foo; | |
} | |
public void DoBar(int number) | |
{ | |
if (number == 1) | |
_foo.DoFoo1(); | |
else if (number == 2) | |
_foo.DoFoo2(); | |
} | |
} | |
} |