How to exclude null value when using FsCheck Property attribute?

a 夏天 提交于 2019-12-24 08:58:46

问题


I need to write a simple method that receives a parameter (e.g. a string) and does smth. Usually I'd end up with two tests. The first one would be a guard clause. The second would validate the expected behavior (for simplicity, the method shouldn't fail):

[Fact]
public void DoSmth_WithNull_Throws()
{
    var sut = new Sut();
    Assert.Throws<ArgumentNullException>(() =>
        sut.DoSmth(null));
}

[Fact]
public void DoSmth_WithValidString_DoesNotThrow()
{
    var s = "123";
    var sut = new Sut();
    sut.DoSmth(s); // does not throw
}

public class Sut
{
    public void DoSmth(string s)
    {
        if (s == null)
            throw new ArgumentNullException();

        // do smth important here
    }
}

When I try to utilize the FsCheck [Property] attribute to generate random data, null and numerous other random values are passed to the test which at some point causes NRE:

[Property]
public void DoSmth_WithValidString_DoesNotThrow(string s)
{
    var sut = new Sut();
    sut.DoSmth(s); // throws ArgumentNullException after 'x' tests
}

I realize that this is the entire idea of FsCheck to generate numerous random data to cover different cases which is definitely great.

Is there any elegant way to configure the [Property] attribute to exclude undesired values? (In this particular test that's null).


回答1:


FsCheck has some built-in types that can be used to signal specific behaviour, like, for example, that reference type values shouldn't be null. One of these is NonNull<'a>. If you ask for one of these, instead of asking for a raw string, you'll get no nulls.

In F#, you'd be able to destructure it as a function argument:

[<Property>]
let DoSmth_WithValidString_DoesNotThrow (NonNull s) = // s is already a string here...
    let sut = Sut ()
    sut.DoSmth s // Use your favourite assertion library here...
}

I think that in C#, it ought to look something like this, but I haven't tried:

[Property]
public void DoSmth_WithValidString_DoesNotThrow(NonNull<string> s)
{
    var sut = new Sut();
    sut.DoSmth(s.Get); // throws ArgumentNullException after 'x' tests
}


来源:https://stackoverflow.com/questions/41022463/how-to-exclude-null-value-when-using-fscheck-property-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!