我正在尝试为返回 IO 单子的 haskell 函数编写 HUnit 测试,因为它们执行文件 I/O。有没有办法做到这一点?现在我正在尝试编写一个只返回一个 Bool 的方法,这可以是我的测试
combine :: FilePath -> FilePath -> Bool
combine fp1 fp2 = do
cs <- readFile fp1
let (_,y,z) = strToHuff cs
let _ = writeToFile fp2 z y
(a, b) <- readFromFile fp2
z == a && b == y
但这给了我以下错误:
FileWriter.hs:153:3: Couldn't match type ‘IO b0’ with ‘Bool’ …
Expected type: IO String -> (String -> IO b0) -> Bool
Actual type: IO String -> (String -> IO b0) -> IO b0
In a stmt of a 'do' block: cs <- readFile fp1
In the expression:
do { cs <- readFile fp1;
let (_, y, z) = strToHuff cs;
let _ = writeToFile "try1.txt" z y;
(a, b) <- readFromFile fp2;
.... }
In an equation for ‘combine’:
combine fp1 fp2
= do { cs <- readFile fp1;
let (_, y, z) = ...;
let _ = ...;
.... }
FileWriter.hs:157:3: Couldn't match expected type ‘IO b0’ with actual type ‘Bool’ …
In a stmt of a 'do' block: z == a && b == y
In the expression:
do { cs <- readFile fp1;
let (_, y, z) = strToHuff cs;
let _ = writeToFile "try1.txt" z y;
(a, b) <- readFromFile fp2;
.... }
In an equation for ‘combine’:
combine fp1 fp2
= do { cs <- readFile fp1;
let (_, y, z) = ...;
let _ = ...;
.... }
Compilation failed.