Accessing the value set by `beforeAll` during tests
Here's what I have:
spec :: Spec
spec = do
manager <- runIO newManager
it "foo" $ do
-- code that uses manager
it "bar" $ do
-- code that usees manager
The docs for runIO
suggest that I probably should use beforeAll
instead, since I don't need manager
to build a spec tree, I just need it to run every test, and in my use case it is best to use the same manager for them rather than creating a new one for each test.
If you don't need the I / O output to build the spec tree, beforeAll might be more appropriate for your use case.
beforeAll :: IO a -> SpecWith a -> Spec
But I can't figure out how to access the manager from the tests.
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
source to share
Spec arguments are passed into your block as normal function arguments (look at the associated type of the type class Example
if you want to understand what's going on). Completely standalone 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"
source to share