0

我正在关注Esposito 的 Yesod 教程并尝试围绕 Mirror 示例进行测试。

我的测试来自以下HomeTest.hs文件中的文件yesod init

{-# LANGUAGE OverloadedStrings #-}
module MirrorTest
    ( mirrorSpecs
    ) where

import TestImport
import qualified Data.List as L

mirrorSpecs :: Spec
mirrorSpecs =
    ydescribe "This tests the sample mirror feature" $ do

        yit "loads the mirror page and checks it has correct elements" $ do
            get MirrorR
            statusIs 200
            htmlAllContain "h1" "Mirror test"
            htmlAllContain "label" "Enter your text"

            request $ do
                setMethod "POST"
                setUrl MirrorR
                byLabel "Enter your text" "wooo"

            statusIs 200
            printBody
            htmlCount ".p" 1
            htmlAllContain ".h1" "You posted"
            htmlAllContain ".p" "woooooow"
            htmlAllContain ".p" "text/plain"

同时我的mirror.hamlet文件是:

<h1> Mirror test
<form method=post action=@{MirrorR}>
    <label for=content>Enter your text
    <input type=text name=content>
    <input type=submit>

但我得到的测试输出是:

1) This tests the sample mirror feature loads the mirror page and checks it has correct elements
More than one input with id content

我很困惑:只有一个输入具有内容名称,而多个元素具有该名称,但据我回忆,名称不一定是唯一的(与实际ids不同)。我是否需要使用Yesod.Test.TransversingCSS来完成我想要在这里做的事情,通过给输入一个实际的 id?

我的 Haskell 仍然很弱,所以我可能遗漏了明显的内容,非常感谢如何在 Yesod 中实现测试的示例。

4

2 回答 2

1

我没有收到与您相同的错误消息。相反,我看到的错误消息是:

没有带有 id 内容的输入

这可能是由于不同的 yesod-form 版本。此错误消息是完全准确的,并指示一个真正的错误。标签的for属性是指标签的id,而不是它的name。请尝试在您的to上设置id属性,看看是否能解决您的问题。inputcontent

于 2014-07-24T08:03:58.310 回答
0

目前,我的解决方案是忽略尝试实际使用第一个 GET 请求中的表单(因为我会使用 Capybara 来处理它),而我只是将参数直接分流到提交表单的 POST 中。这是正在传递的:

{-# LANGUAGE OverloadedStrings #-}
module MirrorTest
    ( mirrorSpecs
    ) where

import TestImport
import qualified Data.List as L

mirrorSpecs :: Spec
mirrorSpecs =
    ydescribe "This tests the sample mirror feature" $ do

        yit "loads the mirror page and checks it has correct elements" $ do
            get MirrorR
            statusIs 200
            htmlAllContain "h1" "Mirror test"
            htmlAllContain "label" "Enter your text"

            request $ do
                setMethod "POST"
                setUrl MirrorR
                addPostParam "content" "wooo"

            statusIs 200
            printBody
            htmlCount "p" 2
            htmlAnyContain "h1" "You posted"
            htmlAnyContain "p" "woooooow"

我仍然很想了解 byLabel 的工作原理以及我在第一次尝试时做错了什么。我很乐意标记一个有帮助的正确答案。

于 2014-07-24T02:49:30.393 回答