我有这个记录:
type Client =
{ Name : string; Income : int ; YearsInJob : int
UsesCreditCard : bool; CriminalRecord : bool }
要为此记录创建生成器,我尝试了以下代码:
let chooseFromList xs =
gen {
let! idx = Gen.choose(0, List.length xs - 1)
return (List.nth xs idx)
}
let generateName = (Gen.oneof [ gen {return "A"};gen {return "B"};gen {return "C"}])
let generateIncome= (chooseFromList [0..5000])
let generate_YearsInJob= chooseFromList [0..45]
let generate_UsesCreditCard = (Gen.oneof [ gen { return true }; gen { return false } ])
let generate_UsesCriminalRecord= (Gen.oneof [ gen { return true }; gen { return false } ])
let genertate_Client =
{
Name= generateName;
Income=generateIncome;
YearsInJob=generate_YearsInJob
UsesCreditCard=generate_UsesCreditCard
CriminalRecord=generate_UsesCriminalRecord
}
问题在于Name= generateName;
我遇到的是erorr:
This expression was expected to have type string,but here has type Gen<string>
并且上一个剩余行存在此错误。
答案 0 :(得分:4)
如果您需要为客户端生成一个生成器,则必须先为您的字段 - 数据生成示例 - 因此gen { ... }
语法会派上用场:
let genertate_Client =
gen {
let! name = generateName
let! income = generateIncome
let! yearsInJob = generate_YearsInJob
let! creditCard = genergenerate_UsesCreditCard
let! criminal = generate_UsesCriminalRecord
return {
Name = name;
Income = income;
YearsInJob = yearsInJob;
UsesCreditCard = creditCard;
CriminalRecord = criminal }
}
当然,你使用计算中的let!
语法从生成器生成示例