简体   繁体   English

如何检查属性[]是否已被使用

[英]How to check if property[] has been used

I use Vs2015 with NSubstitute 3.1.0.我将 Vs2015 与 NSubstitute 3.1.0 一起使用。 This code is passed but shouldn't.此代码已通过,但不应通过。

public class Test
{
    public string[] Content { get; set; }
}
[Fact]
public void Should_What()
{
    var obj = Substitute.For<Test>();
    var test = obj.DidNotReceive().Content;
    var test2 = obj.Received().Content;
}

If replace class Test to interface it will works well, but in my test I have class.如果将 class Test替换为接口,它将运行良好,但在我的测试中,我有 class。

How to check if property Content has been used?如何检查属性 Content 是否已被使用?

The solution is to use virtual for the property.解决方案是对属性使用虚拟。

public virtual string[] Content { get; set; }

But, if two opposing tests return the True at the same time, we have a bug.但是,如果两个相反的测试同时返回 True,我们就有了 bug。 Can explain why and indicate a solution, but this is a workaround.可以解释原因并指出解决方案,但这是一种解决方法。

In my opinion it should throw an exception (ex. NotSupportedException, when a test is executed on non-virtual properties).在我看来,它应该抛出一个异常(例如 NotSupportedException,当对非虚拟属性执行测试时)。 The testing environment is expected to detect the problems.测试环境预计会检测到问题。 So it should be pessimistic.所以应该是悲观的。 In this case should be better to return false instead of true.在这种情况下应该更好地返回 false 而不是 true。 To sign the developer the problem (even if it is in the framework).给开发者签名问题(即使它在框架中)。 For example in SQL when try to check null value, will always return false (null == true, null == 0 or even null == null). For example in SQL when try to check null value, will always return false (null == true, null == 0 or even null == null). This is just my point.这只是我的观点。 What do you guys think about it?你们怎么看?

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

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