Как я могу игнорировать значение случая дискриминированного объединения в утверждении FsUnit?Как игнорировать значение случая дискриминированного объединения в утверждении FsUnit?
Возьмем, к примеру:
В моем тесте, я хочу сделать это:
let display = balance |> select Pepsi
display |> should equal Denied
Я не возражал бы делать это:
display |> should equal (Denied _)
Однако я При этом это делается:
display |> should equal (Denied {Deposited=0.25; Requires=1.00})
Обратите внимание, как я должен быть с выражением выше.
Следовательно, я просто хочу знать, было ли это отказано. Меня не интересуют детали.
Вот фактический тест:
[<Test>]
let ``Vending machine reflects more money required for selection``() =
// Setup
let balance = Quarter |> insert []
// Test
let display = balance |> select Pepsi
// Verify
display |> should equal (Denied {Deposited=0.25; Requires=1.00})
Вот функция:
let select product balance =
let attempt = { Deposited=balance
Requires=product |> getPrice }
let paidInFull = attempt.Deposited >= attempt.Requires
if not paidInFull then
Denied attempt
else Granted product
Вот весь домен:
module Machine
type Deposit =
| Nickel
| Dime
| Quarter
| OneDollarBill
| FiveDollarBill
type TransactionAttempt = {
Deposited:float
Requires:float
}
type State =
| OutOfService
| PaymentReceived of Deposit
| WaitingForSelection
| NotPaidInFull of TransactionAttempt
type Product =
| Pepsi
| Coke
| Sprite
| MountainDew
type RequestResult =
| Denied of TransactionAttempt
| Granted of Product
(* Functions *)
open System
let display = function
| OutOfService -> "Out of Service"
| WaitingForSelection -> "Make selection"
| NotPaidInFull attempt -> sprintf "%s Required" ((attempt.Requires - attempt.Deposited).ToString("C2"))
| PaymentReceived deposit -> match deposit with
| Nickel -> "5¢"
| Dime -> "10¢"
| Quarter -> "25¢"
| OneDollarBill -> "$1.00"
| FiveDollarBill -> "$5.00"
let getBalance coins =
coins |> List.fold (fun acc d -> match d with
| Nickel -> acc + 0.05
| Dime -> acc + 0.10
| Quarter -> acc + 0.25
| OneDollarBill -> acc + 1.00
| FiveDollarBill -> acc + 5.00) 0.00
let insert balance coin =
coin::balance |> getBalance
let getPrice = function
| Pepsi -> 1.00
| Coke -> 1.00
| Sprite -> 1.00
| MountainDew -> 1.00
let select product balance =
let attempt = { Deposited=balance
Requires=product |> getPrice }
let paidInFull = attempt.Deposited >= attempt.Requires
if not paidInFull then
Denied attempt
else Granted product
Ответ от @rmunn выглядит хорошо, но что не так с 'display |> должно равняться (отрицается {Depositited = 0.25; Requires = 1.00})'? Должен ли 'Отрицать {Депонировано = 0,22; Requires = 1.04} 'пройти тест? –
Спасибо, Марк. Ты прав. –