这是我得到的:
spec :: Spec
spec = do
manager <- runIO newManager
it "foo" $ do
-- code that uses manager
it "bar" $ do
-- code that usees manager
对于文档runIO
表明,我或许应该使用beforeAll
替代,因为我并不需要manager
对构建规范的树,我只是需要它来运行每个测试,而在我的使用情况下,它是更好地为他们都有着相同的管理者,而不是创建每个测试一个新的。
如果您不需要IO操作的结果来构造规格树,那么beforeAll可能更适合您的用例。
beforeAll :: IO a -> SpecWith a -> Spec
但是我不知道如何从测试中访问管理器。
spec :: Spec
spec = beforeAll newManager go
go :: SpecWith Manager
go = do
it "foo" $ do
-- needs "manager" in scope
it "bar" $ do
-- needs "manager" in scope
Spec参数作为常规函数参数传递到您的it块(Example
如果您想了解发生了什么,请查看类型类的关联类型)。一个完全独立的示例是:
import Test.Hspec
main :: IO ()
main = hspec spec
spec :: Spec
spec = beforeAll (return "foo") $ do
describe "something" $ do
it "some behavior" $ \xs -> do
xs `shouldBe` "foo"
it "some other behavior" $ \xs -> do
xs `shouldBe` "foo"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句