属性的单元测试体

Unit testing body of properties

这是我的代码

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;
        }
    }

如您所见,我在我的 Set 正文中进行了验证。我需要测试一下。 我尝试在我的测试中调用构造函数。但是我在断言之前在构造函数( act )中得到异常。如何使我的测试正确?

这是我的测试:

[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) },
            };
        }
    }

我收到有关此行的错误:

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

构造函数正在尝试设置 属性 以便它获得异常。并且没有得到断言。

我的问题是:如何在不更改构造函数的情况下进行测试?

我不熟悉 XUnit 语法,但我认为您需要删除 "manager.Invoking" 行,因为异常将在上面的行中抛出,然后用测试本身的 ExpectedException 属性替换它.

类似于:

[ExpectedException(typeof(Exception))]
[MemberData("RequestFakeData")]
public void Should_Throw_Exception_RequestDate......

此外,我建议将 Exception 切换为 ArgumentException。

编辑: 我不确定以下语法在 lambda 语句中是否正确,但这应该是一个好的开始。

[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)
}

我从以下内容推断出这个答案: http://hadihariri.com/2008/10/17/testing-exceptions-with-xunit/

我找到了解决方案,对于那些稍后检查的人,我把它放在这里。

如果我们想在 xUnit 中测试这些类型的异常,可能会有点棘手。

我在这里读了一篇文章:http://hadihariri.com/2008/10/17/testing-exceptions-with-xunit/

它帮助我编辑代码并获得正确答案。

我这样更改了我的测试:

[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) },
            };
        }
    }

希望对您有所帮助。