在测试期间访问由“beforeAll”设置的值
Access a value set up by `beforeAll` during tests
这是我得到的:
spec :: Spec
spec = do
manager <- runIO newManager
it "foo" $ do
-- code that uses manager
it "bar" $ do
-- code that usees manager
runIO
的文档建议我应该改用 beforeAll
,因为我不需要 manager
来 构造 spec 树,我只需要它 运行 每次测试,在我的用例中,他们最好共享同一个管理器,而不是为每个测试创建一个新的管理器.
If you do not need the result of the IO action to construct the spec tree, beforeAll may be more suitable for your use case.
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
类型 class 的关联类型)。一个完全独立的例子是:
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"
这是我得到的:
spec :: Spec
spec = do
manager <- runIO newManager
it "foo" $ do
-- code that uses manager
it "bar" $ do
-- code that usees manager
runIO
的文档建议我应该改用 beforeAll
,因为我不需要 manager
来 构造 spec 树,我只需要它 运行 每次测试,在我的用例中,他们最好共享同一个管理器,而不是为每个测试创建一个新的管理器.
If you do not need the result of the IO action to construct the spec tree, beforeAll may be more suitable for your use case.
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
类型 class 的关联类型)。一个完全独立的例子是:
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"