我试图弄清楚为什么使用时,包含定义为模块的单元测试的测试文件会失败stack build --test
。
说从头开始定义一个简单的测试模块,方法是:
stack new test-module
cd test-module
vim package.yaml # remove "executables" section, add "hspec" as tests dependency
按照Hspec文档中的“入门”说明,我修改了以下文件:
-- file test/Spec.hs
module LibSpec where
import Test.Hspec
import Lib
main :: IO ()
main = hspec $ do
describe "divides" $ do
it "returns True when the first number divides the second" $
2 `divides` 4 `shouldBe` True
-- file src/Lib.hs
module Lib (divides) where
divides :: Integer -> Integer -> Bool
divides d n = rem n d == 0
运行stack build --test
会引发以下错误:
<no location info>: error:
output was redirected with -o, but no output will be generated
because there is no Main module.
当我从test/Spec.hs
文件中注释掉“模块定义”行时,构建成功并且单元测试通过:
-- file test/Spec.hs
-- Notice the next line is commented out:
-- module LibSpec where
import Test.Hspec
import Lib
main :: IO ()
main = hspec $ do
describe "divides" $ do
it "returns True when the first number divides the second" $
2 `divides` 4 `shouldBe` True
是Hspec相关还是Stack相关?还是我缺少明显的东西?
它是Haskell语言的一部分。
Haskell程序是模块的集合,根据惯例,其中一个模块必须被调用
Main
并且必须导出valuemain
。允许使用仅由模块主体组成的模块的缩写形式。如果使用此格式,则假定标头为
module Main(main) where
。
Haskell 2010报告,第5节(模块)https://www.haskell.org/onlinereport/haskell2010/haskellch5.html#x11-980005
另请参阅cabal文档,以了解有关package.yaml
作为代理的配置的包含测试/可执行文件的字段的信息:
main-is
:(...),尽管文件名可能有所不同,但模块本身必须命名为Main。
https://www.haskell.org/cabal/users-guide/developing-packages.html#pkg-field-executable-main-is
GHC可以选择-main-is MyModule.mymain
覆盖此行为(在GHC用户指南中记录)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句