【问题标题】:Unit testing body of properties单元测试属性体
【发布时间】:2016-12-06 07:53:30
【问题描述】:

这是我的代码

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

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

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

【问题讨论】:

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


    【解决方案1】:

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

    如果我们想在 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) },
                };
            }
        }
    

    希望对您有所帮助。

    【讨论】:

    • 你为什么按照问题中的要求测试构造函数而不是属性?您可以使用无参数构造函数创建实例并测试属性
    【解决方案2】:

    我不熟悉 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 中寻找类似的东西,但到目前为止我一无所获。它仅适用于 VisualStudio 测试。不是 xUnit。
    • 抱歉,我自己对 XUnit 的无知显示在那里。让我知道以上是否有任何改变。
    • 非常感谢。因为这让我想到了我的错误并帮助我以另一种方式解决它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 2014-08-16
    • 2022-01-23
    相关资源
    最近更新 更多