将测试文件定义为模块时,使用hspec定义的通过堆栈调用的测试会引发错误

麦克罗西

我试图弄清楚为什么使用时,包含定义为模块的单元测试的测试文件会失败stack build --test

说从头开始定义一个简单的测试模块,方法是:

stack new test-module
cd test-module

vim package.yaml # remove "executables" section, add "hspec" as tests dependency

按照Hspec文档中的入门”说明,我修改了以下文件:

步骤1:描述您想要的行为

-- 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

第2步:编写一些代码

-- 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相关?还是我缺少明显的东西?

Li-yao Xia

它是Haskell语言的一部分。

Haskell程序是模块的集合,根据惯例,其中一个模块必须被调用Main并且必须导出value main

允许使用仅由模块主体组成的模块的缩写形式。如果使用此格式,则假定标头为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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通用在Java中引发错误的定义方法时

添加自定义LinearLayout时,Android布局预览器会引发错误

如何使用定义为常量的文件路径测试Go代码

邮递员测试引发错误:TypeError:无法读取未定义的属性“ ProjectNumber”

强制模块模拟在测试中引发错误

即使该方法引发错误,Rspec的raise_error测试也会失败

测试模块移至单独文件时,宏未定义

redux-saga的单元测试会引发错误:必须在迭代器上调用runSaga

在文件顶部使用配置块会引发错误

使用react-hooks-testing-library测试自定义钩子会引发错误

引发错误时,单元测试无法通过

Angular 6/7消耗自定义库会引发错误TS2397:找不到模块'*'

在.then()内部调用函数会引发错误

RSpec测试销毁控制器操作会引发错误:ActionView :: MissingTemplate

尝试使用用于JMeter的docker容器(非GUI模式)运行JMeter测试会引发错误“无法打开xxxx.jmx文件”

覆盖JavaScript函数会引发错误“某些模块无法启动”和“未定义网络”

Android Kotlin Espresso断言单元测试中的预期活动会引发错误

Rspec:如何测试引发错误的方法

使用iotedgehubdev调试Azure IoT模块会引发错误

将文件夹ID字符串传递到调用DriveApp.getFolderById(id).getDateCreated()的自定义函数时,Apps Script引发错误。

单元测试时,如何访问使用Spock通过属性文件访问的全局定义的属性

JSP,Integer.parseInt即使在测试为null时也会引发错误

使用硒测试无头Firefox,但会引发错误

测试函数不会引发错误

使用Selenium的while函数循环会引发错误NameError:未定义名称'neadaclick'

使用布尔指针作为参数定义 virtual void 会引发错误

使用定义的运算符时,双嵌套括号在宏内部引发错误

@wordpress/scripts 添加自定义 js 文件会引发错误

PHPUnit - 错误:调用测试文件中未定义的函数