Dan*_*ton 6 haskell unit-testing hspec
这是我得到的:
spec :: Spec
spec = do
manager <- runIO newManager
it "foo" $ do
-- code that uses manager
it "bar" $ do
-- code that usees manager
Run Code Online (Sandbox Code Playgroud)
对于文档runIO表明,我或许应该使用beforeAll替代,因为我并不需要manager对构建规范的树,我只是需要它来运行每个测试,而在我的使用情况下,它的所有份额对他们来说是更好的同一个经理而不是创造每个测试都有一个新的.
如果您不需要IO操作的结果来构造spec树,则beforeAll可能更适合您的用例.
beforeAll :: IO a -> SpecWith a -> Spec
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何从测试中访问管理器.
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
Run Code Online (Sandbox Code Playgroud)
Spec参数作为常规函数参数传递给它的块(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"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
201 次 |
| 最近记录: |