7

这是我所拥有的:

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
4

1 回答 1

10

规范参数作为常规函数参数传递给您的 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"
于 2015-05-31T06:15:51.950 回答