单元测试属性体

时间:2016-08-01 09:30:05

标签: c# asp.net-mvc unit-testing theory xunit

这是我的代码

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)中得到了Exception。如何使我的测试正确?

这是我的测试:

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

构造函数正在尝试设置属性,以便获取Exception。并没有得到断言。

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

2 个答案:

答案 0 :(得分:5)

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

如果我们想在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) },
            };
        }
    }
希望它会有所帮助。

答案 1 :(得分:2)

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

类似的东西:

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

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

编辑: 我不能确定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/

相关问题