首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >执行elm-test引发运行时异常:无法读取未定义的属性href。

执行elm-test引发运行时异常:无法读取未定义的属性href。
EN

Stack Overflow用户
提问于 2017-07-04 15:38:24
回答 1查看 103关注 0票数 0

运行时异常的测试结果如下:

代码语言:javascript
复制
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) True
代码语言:javascript
复制
TypeError: 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上找到。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-07 21:02:15

我不知道你是否在埃尔姆·斯拉克身上找到了答案,但我花了一些时间研究这个问题,并找到了问题的原因。

简而言之,问题是将对Navigation.program的调用包含在一个Elm文件中,该文件使用elm-test运行。在您的例子中,Navigation.programHome.elm中。将其移动到另一个模块(或暂时将其注释掉),您的测试就会运行。

更详细地说,Navigation.program后面的本机代码获取document.location并将其传递给您的init函数。但是,在运行elm测试时,您没有document对象,因为没有浏览器。elm将构成一个框架document对象,但是这个对象没有location属性,因此会抛出一个试图获取location.href的异常。

埃尔姆试验包文档(在“特定于应用程序的技术”下的第1点)中有一些这方面的指导:

避免导入Main模块。大部分代码属于其他模块,所以请导入这些模块。

或者,elm测试版本中的已经被多层填充为0.18.9,因此升级到该版本也将解决这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44909872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档