Snippets

Asserting exceptions

Customer.cs

public class Customer
{
    /// <summary>
    /// The method returns an ArgumentException if there is an error if not 
    /// it returns 100
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public int GetOrdersByName(string name)
    {
        if (string.IsNullOrEmpty(name))
        {
            throw new ArgumentException("Error");
        }
        return 100;
    }

    public string Name => "Hugh";
    public int Age => 32;
}

CustomerTest.cs

public class CustomerTest
{
    [Fact]
    public void CheckNameNoEmpty()
    {
        var customer = new Customer();
        Assert.NotNull(customer.Name);
        Assert.False(String.IsNullOrEmpty(customer.Name));
    }

    [Fact]
    public void CheckLegitForDiscount()
    {
        var customer = new Customer();
        Assert.InRange(customer.Age, 25, 40);
    }

    [Fact]
    public void GetOrdersByNameNoNull()
    {
        var customer = new Customer();
        var exeptionDetails = Assert.Throws<ArgumentException>(() => customer.GetOrdersByName(null));
        Assert.Equal("Error", exeptionDetails.Message);
    }
}

Asserting collections

public List<int> Fibbonacci => new List<int> { 1, 1, 2, 3, 5, 8, 13 };
/// <summary>
/// It should returns true because we don't have any 0 value in the
/// List of numbers
/// </summary>
[Fact]
public void FiboDoesNotIncludeZero()
{
    var calc = new Calculator();
    Assert.All(calc.FiboNumbers, n => Assert.NotEqual(0, n));
}

.All method verifies that all items in the collection pass when executed against action.

[Fact]
public void FiboIncludes13()
{
    var calc = new Calculator();
    Assert.Contains(13, calc.FiboNumbers);
}

.Contains method verifies that a collection contains a given object.

[Fact]
public void FiboDoesNotIncludes4()
{
    var calc = new Calculator();
    Assert.DoesNotContain(4, calc.FiboNumbers);
}

.DoesNotContain method verifies that a collection does not contain a given object.

public static class CustomerFactory
{
    public static Customer CreateCustomerInstance(int orderCount)
    {
        if (orderCount <= 100)
            return new Customer();
        else
            return new LoyalCustomer();
    }
}

IsType is a method that verifies that an object is exactly the given type (and not a derived type)

/// <summary>
/// IsType is a method that verifies that an object is exactly the given type (and not a derived type)
/// Returns: The object, casted to type T when successful.
/// </summary>
[Fact]
public void LoyalCustomerForOrdersG100()
{
    var customer = CustomerFactory.CreateCustomerInstance(102);
    Assert.IsType<LoyalCustomer>(customer);
}

Asserting object types, customer.cs

public class LoyalCustomer: Customer
{
    public int Discount { get; set; }
    public LoyalCustomer()
    {
        Discount = 10;
    }

    public override int GetOrdersByName(string name)
    {
        return 101;
    }
}

public static class CustomerFactory
{
    public static Customer CreateCustomerInstance(int orderCount)
    {
        if (orderCount <= 100)
            return new Customer();
        else
            return new LoyalCustomer();
    }
}

CustomerTest.cs

/// <summary>
/// IsType is a method that verifies that an object is exactly the given type (and not a derived type)
/// Returns: The object, casted to type T when successful.
/// </summary>
[Fact]
public void LoyalCustomerForOrdersG100()
{
    var customer = CustomerFactory.CreateCustomerInstance(102);
    var loyalCustomer = Assert.IsType<LoyalCustomer>(customer);
    Assert.Equal(10, loyalCustomer.Discount);
}

[Fact]
public void LoyalCustomerForOrdersG100Fail()
{
    var customer = CustomerFactory.CreateCustomerInstance(90);
    Assert.IsType<Customer>(customer);
}

Last updated