我有一个内存中的存储库,可以通过调用此函数来创建:
newEmptyRepository :: IO InMemoryGameRepository其中InMemoryGameRepository的定义如下:
type State = (HashMap GameId Game)
type IORefState = IORef State
newtype InMemoryGameRepository = InMemoryGameRepository IORefState在为我的Scotty应用程序编写测试时,我看到了使用这种方法的示例:
spec =
before app $ do
describe "GET /" $ do
it "responds with 200" $ get "/" `shouldRespondWith` 200
it "responds with 'hello'" $ get "/" `shouldRespondWith` "hello"
...这一切都很好,但我还需要以某种方式初始化InMemoryGameRepository (通过调用newEmptyRepository),并在我的测试中使用创建的实例。因此,我将app更改为:
app :: InMemoryGameRepository -> IO Application
app repo = scottyApp $ routes repo我正在尝试创建一个使用存储库和IO Application的测试,例如这样(它不起作用):
spec =
before (do repo <- newEmptyRepository
app repo) $
-- API Tests
describe "GET /api/games" $
it "responds with " $ do
liftIO $ startGame repo
get "/api/games" `shouldRespondWith` singleGameResponse其中startGame的定义如下:
startGame :: InMemoryGameRepository -> IO Game在这里,编译器说明(显然) repo不在作用域内。但是我如何才能做到这一点呢?也就是说,我想在app和测试中共享一个newEmptyRepository实例。
Ps:你可以在github上看到完整的应用程序。
发布于 2017-12-19 00:29:29
应该使用beforeWith,它的类型为
beforeWith :: (b -> IO a) -> SpecWith a -> SpecWith b例如,将其用作类型为SpecWith Application -> Spec的before newEmptyRepository . beforeWith app。
如果您希望在测试用例中同时访问InMemoryGameRepository和Application,请定义一个帮助函数
withArg f a = (,) a <$> f a
withArg :: Functor f => (t -> f b) -> t -> f (t, b)然后使用
before newEmptyRepository . beforeWith (withArg app)
:: SpecWith (InMemoryGameRepository, Application) -> Spec最后,您不应该在测试的定义中使用liftIO $ startGame repo -这将在每次构建测试树时运行startGame (尽管这可能正是您想要的,但看起来并不是这样)。相反,如果使用before系列函数,startGame将在测试实际运行之前运行一次。您甚至可以使用与上面相同的技术访问startGame返回的Game:
before newEmptyRepository
. beforeWith (withArg startGame)
. beforeWith (withArg $ app . fst)
:: SpecWith ((InMemoryGameRepository, Game), Application) -> Spechttps://stackoverflow.com/questions/47843958
复制相似问题