这是我得到的:
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
-- …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用HSpec和QuickCheck来验证Monoid的属性(关联性和标识元素)。我将验证特定的实例,但想保留大多数代码的多态性。这是几个小时后我想到的:
module Test where
import Test.Hspec
import Test.QuickCheck
import Data.Monoid
instance (Arbitrary a) => Arbitrary (Sum a) where
arbitrary = fmap Sum arbitrary
instance (Arbitrary a) => Arbitrary (Product a) where
arbitrary = fmap Product arbitrary
prop_Monoid_mappend_mempty_x x = mappend mempty x === x
sumMonoidSpec = it "mappend mempty x = x" $ property (prop_Monoid_mappend_mempty_x :: Sum Int -> Property)
productMonoidSpec = it "mappend mempty x = x" $ property (prop_Monoid_mappend_mempty_x :: Product Double -> Property)
main :: IO () …Run Code Online (Sandbox Code Playgroud) 我有以下代码用于在以下网站上创建挑战:codewars
describe "Random cases" $ do
it "It should handle random test cases" $
property $ prop_check where
prop_check (Positive x) = solution x == ref_sol x
--- ref_sol function
Run Code Online (Sandbox Code Playgroud)
我想将xprop_check 中的值设置为大于 4 的正整数,并且最多为五位数字(不超过五位数字,即:最大值 = 99999)。
我该如何接近它呢?
我正在学习Haskell并且我已经编写了这个函数:
safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:xs) = Just x
Run Code Online (Sandbox Code Playgroud)
我现在正尝试用HSpec测试它:
import Test.Hspec
main :: IO ()
main = hspec spec
spec :: Spec
spec =
describe "safeHead" $
it "should return Nothing for empty list" $
safeHead [] `shouldBe` Nothing
Run Code Online (Sandbox Code Playgroud)
但是无法编译:
Error:(14, 19) ghc: No instance for (Eq a0) arising from a use of ‘shouldBe’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance Eq a => Eq (Maybe …Run Code Online (Sandbox Code Playgroud) 与quickcheck如何支持反例类似:
property \x ->
counterexample ("Foo failed with: " ++ ...) $
foo x
Run Code Online (Sandbox Code Playgroud)
但以某种方式与配合使用shouldBe,例如
failDetails (" details: " ++ baz a) $
a `shouldBe` 2
Run Code Online (Sandbox Code Playgroud)
我希望它按照以下方式打印一些内容:
expected: 2
but got: 3
details: ...
Run Code Online (Sandbox Code Playgroud) 在大多数编程语言中,在某些情况下很容易跳过测试。在基于 haskell HSpec 的测试套件中是否有正确的方法来做到这一点?
I have found other questions on similar lines but nothing that answers my question in this particular scenario. Furthermore, there seem to be few resources which succinctly cover the subject of unit testing IO actions in Haskell.
Let's say I have this typeclass for my database communication:
data Something = Something String deriving Show
class MonadIO m => MonadDB m where
getSomething :: String -> m Something
getSomething s = do
... -- assume a DB call is made and …Run Code Online (Sandbox Code Playgroud) module ExercisesSpec where
import Test.Hspec
import Test.QuickCheck
import Control.Exception (evaluate)
halve :: [a] -> ([a], [a])
halve xs = splitAt (length xs `div` 2) xs
main :: IO ()
main = hspec $ do
describe "halve" $ do
it "0 elements" $ do
halve [] `shouldBe` ([],[])
it "1 element" $ do
halve [1] `shouldBe` ([],[1])
it "2 elements" $ do
halve [1,2] `shouldBe` ([1],[2])
it "3 elements" $ do
halve [1,2,3] `shouldBe` ([1],[2,3])
it …Run Code Online (Sandbox Code Playgroud) 如何在 HSpec 中指定测试套件?我将为我*.hs的每个模块创建多个测试文件,我只想运行stack test并运行所有测试。我该如何设置?
我试图在我的 cabal 文件中列出这样的测试模块,但它不起作用:
test-suite foo-test
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Spec.hs
, Mod1Spec.hs
build-depends: base
, containers >= 0.5.6.2
, hqfl
, hspec >= 2.2.3
, hspec >= 2.2.3
, mtl >= 2.2.1
, pipes >= 4.1.8
, random >= 1.1
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用HSpec在haskell中进行TDD.因此,在为除法运算符编写特殊情况时,例如:
3 / 0 => Infinity0 / 0 => Nan如何使用Hspec测试上述案例?