26
loading...
This website collects cookies to deliver better user experience
public interface IRepository
{
bool IsValid(int id);
void Add(int id, bool valid);
}
var mock = new Mock<IRepository>();
mock.Setup(x => x.IsValid(It.IsAny<int>())).Returns(true);
public class RepositorySpy : IRepository
{
public boolean IsValidWasCalled = false;
public Boolean bool IsValid(int id)
{
IsValidWasCalled = true;
return true;
}
}
IsValidWasCalled
was true.var spy = new RepositorySpy();
Assert.True(spy.IsValidWasCalled);
var mock = new Mock<IRepository>();
mock.Setup(x => x.IsValid(1)).Returns(true);
mock.Setup(x => x.IsValid(2)).Returns(false);
mock.Verify(x => x.Add(1), Times.Once);
mock.Verify(x => x.IsValid(It.IsAny<int>())), Times(2));
public class FakeRepository : IRepository
{
private Dictionary<int, bool> _values = new Dictionary<int, bool>();
public bool IsValid(int id)
{
return _values[id];
}
public void Add(int id, bool valid)
{
_values.Add(id, open);
}
}