# 在Xunit中使用FsCheck

## 目录

Install-Package FsCheck.Xunit -Version 2.13.0


private int Add(int x, int y)
{
return x + y;
}


[Property]
public bool Commutative(int x, int y)
{
}


F#

[<Property>]
let Commutative x y =


#### 1. 不同的执行顺序，同样的执行结果

 [Property]
{
var result1 = list.OrderBy(x => x).Select(Add1);
var result2 = list.Select(Add1).OrderBy(x => x);

return result1.SequenceEqual(result2);
}


F#

 [<Property(Verbose=true)>]
let +1 then sort should be same as sort then +1 aList =
let add1 x = x + 1

let result1 = aList |> List.sort |> List.map add1
let result2 = aList |> List.map add1 |> List.sort

result1 = result2


#### 2.连续执行操作，结果跟之前一致

[Property]
public bool ReverseThenReverseShouldSameAsOriginal(int[] list)
{
var result=  list.Reverse().Reverse();
return result.SequenceEqual(list);
}


F#

[<Property>]
let reverse then reverse should be same as original
(aList:int list) =
let reverseThenReverse = aList |> List.rev |> List.rev
reverseThenReverse = aList


#### 3. 有一些属性是永远不会改变的

public bool SomethingNeverChanged(List<int> list)
{
var result = list.OrderBy(x => x);
return result.Count() == list.Count;
}


F#

let sort should have same length as original (aList:int list) =
let sorted = aList |> List.sort
List.length sorted = List.length aList


## 为OO代码编写Property-based测试

public class Dollar
{
private int _amount;

public Dollar(int amount)
{
_amount = amount;
}

public int Amount => _amount;

{
}

public void Multiplier(int multiplier)
{
_amount = _amount * multiplier;
}

public static Dollar Create(int amount)
{
return new Dollar(amount);
}
}


F#

type Dollar(amount : int) =
let mutable privateAmount = amount;

member this.Amount = privateAmount
member this.Times multiplier =
privateAmount <- this.Amount * multiplier
static member Create amount =
Dollar amount


[Property]
public bool SetAndGetShouldGiveSameResult(int amount)
{
var dollar = Dollar.Create(0);

return dollar.Amount == amount;
}


F#

[<Property>]
let set then get should give same result value =
let obj = Dollar.Create 0
let newValue = obj.Amount
value = newValue


[Property]
public bool AddThenMultiplierSameAsCreate(int start, int times)
{
var dollar = Dollar.Create(0);
dollar.Multiplier(times);

var dollar2 = Dollar.Create(start * times);

return dollar.Amount == dollar2.Amount;
}


F#

 [<Property>]
let add then multiplier same as create value times =
let dollar = Dollar.Create 0
dollar.Times times

let dollar2 = Dollar.Create(value*times);

dollar.Amount = dollar2.Amount


## 编写自定义Generator

public class User
{
public string Name { get; set; }

public int Age { get; set; }
}


public class UserArbitrary: Arbitrary<User>
{
public override Gen<User> Generator =>
from x in Arb.Generate<string>()
from int y in Gen.Choose(20, 30)
where x != string.Empty
select new User {Name = x, Age = y};
}


public class MyGenerators {
public static Arbitrary<User> User() {
return new UserArbitrary();
}
}

Arb.Register<MyGenerators>();


[Property]
public bool GenerateUsers(User user)
{
return user.Name != string.Empty;
}


posted @ 2019-03-10 19:01  .NET西安社区  阅读(376)  评论(0编辑  收藏  举报