Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing body of properties

This is my Code

public class AssistanceRequest : DocumentBase
{
    public AssistanceRequest()
    {
        RequestTime = DateTime.Now;
        ExcecutionTime = DateTime.MaxValue;
    }

    public AssistanceRequest(int amount, string description, DateTime? requestTime) : this()
    {
        //Check for basic validations
        if (amount <= 0)
            throw new Exception("Amount is not Valid");
        this.Amount = amount;
        this.Description = description;
        this.RequestTime = requestTime ?? DateTime.Now;
    }

    private long Amount { get; set; }

    private DateTime requestTime { get; set; }

    public DateTime RequestTime
    {
        get { return requestTime; }
        set
        {
            if (value != null && value < DateTime.Now)
                throw new Exception("Request Time is not Allowed");
            requestTime = value;
        }
    }

As you can see I have a validation in my Set body. I need to test it. and I try to call the constructor in my Tests. but I get Exception in Constructor ( act ) before assertion. How to make my tests right?

This is my Test:

[Theory]
    [MemberData("RequestFakeData")]
    public void Should_Throw_Exception_RequestDate(int amount, string description, DateTime requestDate)
    {
        var manager = new AssistanceRequest(amount,description,requestDate);
        manager.Invoking(x => x.SomeMethodToChangeRequestTime).ShouldThrowExactly<Exception>()
    }

    public static IEnumerable<object[]> RequestFakeData
    {
        get
        {
            // Or this could read from a file. :)
            return new[]
            {
            new object[] { 0,string.Empty,DateTime.Now.AddDays(1) },
            new object[] { 2,"",DateTime.Now.AddDays(-2) },
            new object[] { -1,string.Empty,DateTime.Now.AddDays(-3) },
            };
        }
    }

I get the Error in about this Line:

var manager = new AssistanceRequest(amount,description,requestDate);

the constructor is trying to set the property so it gets the Exception. and does not get to the assertion.

My question is: How can I test this without changing my constructor?

like image 437
MRebati Avatar asked Jan 06 '23 15:01

MRebati


1 Answers

I found the solution and for those who check later on I put it right here.

If we want to test these types of Exceptions in xUnit it may get a little tricky.

I read an article here: http://hadihariri.com/2008/10/17/testing-exceptions-with-xunit/

it helped me out to edit my Code and get the right answer.

I changed my Tests like this:

[Theory]
    [MemberData("RequestFakeData")]
    public void Should_Throw_Exception_RequestDate(int amount, string description, DateTime requestDate)
    {
        Exception ex = Assert.Throws<Exception>(() => new AssistanceRequest(amount,description,requestDate));
        Assert.Equal("Request Time is not Allowed",ex.Message);
    }

    public static IEnumerable<object[]> RequestFakeData
    {
        get
        {
            return new[]
            {
            new object[] { 1,string.Empty,DateTime.Now },
            new object[] { 2,"",DateTime.Now.AddDays(-2) },
            new object[] { 1,string.Empty,DateTime.Now.AddDays(-3) },
            };
        }
    }

hope it would be helpful.

like image 190
MRebati Avatar answered Jan 15 '23 19:01

MRebati