【发布时间】:2015-05-26 09:48:21
【问题描述】:
我正在用 Haskell 编写一个聊天服务器。我的可执行文件有以下代码:
data Client = Client {idx :: Int, hClient :: Handle} deriving (Show, Eq)
tellClient :: String -> Client -> IO ()
tellClient = flip $ hPutStrLn . hClient
askClient :: Client -> IO (String)
askClient = hGetLine . hClient
我希望能够使用它来写入和读取文件。我正在使用 HSpec 编写针对此代码的测试。我有以下函数来构建临时 Client 对象(依赖于“临时”)库:
withTempClient :: Int -> (Client -> IO a) -> IO a
withTempClient cIdx f = withTempFile "tests/generated" "clientFile.tmp" axn
where axn _ handle = f $ Client cIdx handle
我用这个来测试上面的代码如下:
main = hspec $ do
describe "client IO" $ do
it "can ask and tell" $ withTempClient 1 $ \c -> do
tellClient "hello" c
hFlush $ hClient c
askClient c `shouldReturn` "hello"
但测试失败并出现以下错误:
uncaught exception: IOException of type EOF (test/generated
/clientFile11587.tmp: hGetLine: end of file)
我想可能是我在withTempClient做错了什么,所以我添加了以下测试用例:
it "opens a handle corectly" $ withTempClient 1 $ \(Client _ h) -> do
hIsOpen h `shouldReturn` True
hIsWritable h `shouldReturn` True
但这已经过去了,所以我不确定问题可能是什么。我错过了什么?
【问题讨论】: