运行时异常的测试结果如下:
test "search yields profile" <|
\_ ->
let
-- Setup
location =
Navigation.Location "" "" "" "" "" "" "" "" "" "" ""
( model, _ ) =
Home.init location
-- Test
( newState, _ ) =
model |> Home.update (Search "Scott")
-- Verify
( onlyOne, isContentProvider1 ) =
( (newState.contentProviders |> List.length) == 1
, newState.contentProviders |> List.member contentProvider1
)
in
Expect.equal (onlyOne && isContentProvider1) TrueTypeError: Cannot read property 'href' of undefined
at Object.getLocation (eval at <anonymous> (my_path\AppData\Roaming\npm\node_modules\elm-test\lib\runner.js:84:37),
<anonymous>:1392 6:17)
at Function.eval (eval at <anonymous> (my_path\AppData\Roaming\npm\node_modules\elm-test\lib\runner.js:84:37),
<anonymous>:14127:43)
at A2 (eval at <anonymous> (my_path\AppData\Roaming\npm\node_modules\elm-test\lib\runner.js:84:37),
<anonymous>:92:11)
at eval (eval at <anonymous> (my_path\AppData\Roaming\npm\node_modules\elm-test\lib\runner.js:84:37),
<anonymous>:21270:31)
at eval (eval at <anonymous> (my_path\AppData\Roaming\npm\node_modules\elm-test\lib\runner.js:84:37),
<anonymous>:22757:4)
at my_path\AppData\Roaming\npm\node_modules\elm-test\lib\runner.js:84:32
at Object.evalElmCode (my_path\AppData\Roaming\npm\node_modules\elm-test\lib\runner.js:84:76)
at my_path\AppData\Roaming\npm\node_modules\elm-test\bin\elm-test:196:14
at my_path\AppData\Roaming\npm\node_modules\elm-test\node_modules\graceful-fs\graceful-fs.js:78:16
at tryToString (fs.js:456:3)我的直觉是,Navigaton.Location是背后的例外。不过,我不知道如何解决这个问题。
源代码可以在GitHub上找到。
发布于 2017-07-07 21:02:15
我不知道你是否在埃尔姆·斯拉克身上找到了答案,但我花了一些时间研究这个问题,并找到了问题的原因。
简而言之,问题是将对Navigation.program的调用包含在一个Elm文件中,该文件使用elm-test运行。在您的例子中,Navigation.program在Home.elm中。将其移动到另一个模块(或暂时将其注释掉),您的测试就会运行。
更详细地说,Navigation.program后面的本机代码获取document.location并将其传递给您的init函数。但是,在运行elm测试时,您没有document对象,因为没有浏览器。elm将构成一个框架document对象,但是这个对象没有location属性,因此会抛出一个试图获取location.href的异常。
在埃尔姆试验包文档(在“特定于应用程序的技术”下的第1点)中有一些这方面的指导:
避免导入
Main模块。大部分代码属于其他模块,所以请导入这些模块。
或者,elm测试版本中的已经被多层填充为0.18.9,因此升级到该版本也将解决这个问题。
https://stackoverflow.com/questions/44909872
复制相似问题