Skip to content

Instantly share code, notes, and snippets.

@StefH
Last active February 16, 2022 14:25
Show Gist options
  • Save StefH/421281ef90f9abfeeaaa2b3a3ea261ad to your computer and use it in GitHub Desktop.
Save StefH/421281ef90f9abfeeaaa2b3a3ea261ad to your computer and use it in GitHub Desktop.
FluentIt
using System;
using FluentAssertions;
using Moq;
using Xunit;
namespace TestProject1;
public static class FluentIt
{
public static TValue IsEquivalentTo<TValue>(TValue expectation)
{
Func<TValue, bool> verify = value =>
{
value.Should().BeEquivalentTo(expectation);
return true;
};
return It.Is<TValue>(value => verify(value));
}
}
public class MyTests
{
private readonly Mock<IUserClient> _userClientMock = new Mock<IUserClient>();
private readonly Manager _sut;
public MyTests()
{
_sut = new Manager(_userClientMock.Object);
}
[Fact]
public void MyTestCase()
{
_sut.TestMethod(); // Act
var user = new User
{
Id = 42,
First = "stef",
Last = "test"
};
_userClientMock.Verify(uc => uc.AddUser(FluentIt.IsEquivalentTo(user)), Times.Once); // ⭐ Instead of It.Is<User>(u => u.Id == 42, u.First == "stef" && u.Last = "test")
}
}
public class Manager
{
private readonly IUserClient _client;
public Manager(IUserClient client)
{
_client = client;
}
public void TestMethod()
{
}
}
public class IUserClient
{
public void AddUser(User user)
{
}
}
public class User
{
public int Id { get; set; }
public string First { get; set; }
public string Last { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment