How do I mock code like this in my unit test. I'm using xUnit and Moq in asp.net core 5. I'm new to xUnit and Moq.
var url = configuration.GetSection("AppSettings").GetSection("SmsApi").Value;
The configuration object is already injected into the constructor.
This is what I currently have in my unit test class
public class UtilityTests
{
private readonly Utility sut;
public UtilityTests()
{
var mockConfig = new Mock<IConfiguration>();
var mockConfigSection = new Mock<IConfigurationSection>();
//mockConfigSection.Setup(x => x.Path).Returns("AppSettings");
mockConfigSection.Setup(x => x.Key).Returns("SmsApi");
mockConfigSection.Setup(x => x.Value).Returns("http://example.com");
mockConfig.Setup(x => x.GetSection("AppSettings")).Returns(mockConfigSection.Object);
sut = new Utility(mockConfig.Object);
}
[Fact]
public void SendSmsShdReturnTrue()
{
var fixture = new Fixture();
var result = sut.SendSms(fixture.Create<string>(), fixture.Create<string>());
result.Should().BeTrue();
}
}