我正在尝试编写Haskell函数的HUnit测试,这些函数返回IO monad,因为它们执行文件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.
return (z == a && b == y)
。 - luquiAssertion
只是一个IO ()
,所以你可以这样做。正确的方法可能是这样的:assertEqual "..." z a; assertEqual "..." b y
- 这样你可以说明为什么它们必须相等,当测试失败时,你将更容易找出原因。 - user2407038