简体   繁体   English

单位测试属性的主体

[英]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. 正如您所看到的,我在Set体中进行了验证。 I need to test it. 我需要测试一下。 and I try to call the constructor in my Tests. 我试着在我的测试中调用构造函数。 but I get Exception in Constructor ( act ) before assertion. 但是在断言之前我在构造函数(act)中得到了Exception。 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. 构造函数正在尝试设置属性,以便获取Exception。 and does not get to the assertion. 并没有得到断言。

My question is: How can I test this without changing my constructor? 我的问题是:如何在不改变构造函数的情况下测试它?

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. 如果我们想在xUnit中测试这些类型的异常,可能会有点棘手。

I read an article here: http://hadihariri.com/2008/10/17/testing-exceptions-with-xunit/ 我在这里读了一篇文章: 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. 希望它会有所帮助。

I'm not familiar with XUnit syntax but I would think you need to remove the "manager.Invoking" line as the exception will be thrown on the line above, then replace it with an ExpectedException attribute on the test itself. 我不熟悉XUnit语法,但我认为您需要删除“manager.Invoking”行,因为异常将在上面的行中抛出,然后将其替换为测试本身的ExpectedException属性。

Something like: 就像是:

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

Additionally, I'd recommend switching Exception for ArgumentException. 另外,我建议为ArgumentException切换Exception。

Edit: I can't be sure if the below syntax is correct on the lambda statement, but this should be a good start. 编辑:我不能确定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)
}

I extrapolated this answer from: http://hadihariri.com/2008/10/17/testing-exceptions-with-xunit/ 我从http://hadihariri.com/2008/10/17/testing-exceptions-with-xunit/推断出这个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM