User713522837 posted
I have an unit test written as below
private CustomerConverter _mockAllotmentConverter;
private OrderConverter _mockReleaseConverter;
[SetUp]
public void Setup()
{
_mockCustomerConverter = new CustomerConverter();
_mockOrderConverter = new OrderConverter();
}
[Test]
public void ConvertAll_NullParam_ThrowsException()
{
var sut = GetDefaultSut();
Func<IEnumerable<Contract>> act = () => sut.ConvertAll(null);
act.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("availability");
}
and the implementation for the method called is as below
public IEnumerable<Contract> ConvertAll(IEnumerable<Availability> availability)
{
if (availability == null) throw new ArgumentNullException(nameof(availability));
var contracts = new List<Contract>();
foreach (var available in availability)
{
var availabilities = Convert(available);
contracts.AddRange(availabilities);
}
return contracts;
}
The unit test passes as expected throwing the ArgumentNullException.
However, for the second unit test and the implementation below, the unit test fails as it's not throwing any exception. It does not
even call the method when I debug. I do not see any difference
except that the input parameter is IEnumerable in the first test and is just single item in the second test. How do I get the second test pass? Could anyone please help
[Test]
public void Convert_NullParam_ThrowsException()
{
var sut = GetDefaultSut();
Func<IEnumerable<Contract>> act = () => sut.SecondMethod(null);
act.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("availability");
}
public IEnumerable<Contract> SecondMethod(Availability availability)
{
if (availability == null) throw new ArgumentNullException(nameof(availability));
var ids = availability.Select(a => a.id).Distinct();
foreach (var id in ids)
{
yield return new Contract()
{
Customers = _customerConverter.GetCustomers(id)
Orders = _orderConverter.GetOrders(id)
};
}
}