我正在尝试为简单函数编写一个单元测试,该函数接受一个列表并返回它,
func :: [a] -> [a]
func x = x
使用测试代码来测试它在给定一个空列表时是否按预期工作
emptyListTest :: Test
emptyListTest = TestCase $ assertEqual "for (func [])," [] $ func []
main :: IO Counts
main = runTestTT $ TestList [emptyListTest]
但是,我得到了错误
No instance for (Show a0) arising from a use of `assertEqual'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Show Double -- Defined in `GHC.Float'
instance Show Float -- Defined in `GHC.Float'
instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
-- Defined in `GHC.Real'
...plus 28 others
In the expression: assertEqual "for (func [])," []
In the second argument of `($)', namely
`assertEqual "for (func [])," [] $ func []'
In the expression:
TestCase $ assertEqual "for (func [])," [] $ func []
其他具有非空列表的测试工作正常,并且在通过调用手动测试时该函数工作func []
正常ghci
。
我还注意到,如果我创建一个虚拟类型,并制作一个包含该类型元素的列表(如果这是正确的说法),那么将其传递给测试似乎有效,并且测试通过
data Dummy = Dummy
deriving(Eq, Show)
emptyList :: [Dummy]
emptyList = []
emptyListTest :: Test
emptyListTest = TestCase $ assertEqual "for (func [])," [] $ func emptyList
为什么是这样?有没有办法在不走虚拟类型路线的情况下用空列表测试函数?