[Haskell-beginners] Testing IO based monads and functions

Felipe Almeida Lessa felipe.lessa at gmail.com
Fri Jan 27 02:09:29 CET 2012


On Thu, Jan 26, 2012 at 11:01 PM, David Hinkes <david.hinkes at gmail.com> wrote:
> type UserInfo = (String, String)  -- (name, password)
>
> getMagicNumberFromHTTPServer :: UserInfo -> IO (Maybe Int)
> getMagicNumberFromHTTPServer user = do
>   let curlHTTPRequest = ... --Setup curl HTTP request
>   httpRepsponse <- ... -- Make curl HTTP request
>   return $ tryGetMagicNumberFromResponse httpResponse

One possible way would be having

  class (Functor m, Monad m) => CurlMonad m where
    curlRequest :: ... -> m ...

  instance CurlMonad IO where
    ...

  getMagicNumberFromHttpServer :: CurlMonad m => UserInfo -> m (Maybe Info)
  getMagicNumberFromHttpServer user = do
    httpResponse <- curlRequest ...
    return $ tryGetMagicNumberFromResponse httpResponse

Now on your tests you could have

  data Mock a = ...

  instance CurlMonad Mock where
    ...

where your Mock instance doesn't really use Curl.

HTH, =)


-- 
Felipe.



More information about the Beginners mailing list