我将如何测试 IHP 我的 CanRoute 实例(漂亮 URL 的解析路由)?
How would I test IHP my CanRoute instance (parse route for beautiful URLs)?
如果您想在 IHP 应用程序中使用漂亮的 URL,您需要编写自己的 CanRoute 实例并解析路由字符串。
-- Web.Routes.hs
module Web.Routes where
import Generated.Types
import IHP.RouterPrelude
import Web.Types
instance CanRoute Co2ProducersController where
parseRoute' = do
string "/producers/"
let co2ProducerById = do
id <- parseId
endOfInput
pure ShowCo2ProducerAction {co2ProducerId = Just id, slug = Nothing}
co2ProducerBySlug = do
slug <- parseText
pure ShowCo2ProducerAction {co2ProducerId = Nothing, slug = Just slug}
co2ProducerBySlug <|> co2ProducerById
我将如何进行测试?我在这里看到了一个可以设置整个测试应用程序的示例:https://github.com/digitallyinduced/ihp/blob/2422bbdfa6165231e89b44c2e7d1c68b65f6b3b4/Test/RouterSupportSpec.hs
但我只想测试我是否正确解析路由,而不是发送请求和测试响应。
测试上述代码的简单方法是什么?
我们为自定义路由定义的 CanRoute
的 parseRoute'
函数基本上只是返回一个 attoparsec 解析器。
我们可以使用 attoparsecs parseOnly
到 运行 带有提供的输入字符串的解析器。这是一个例子:
module Test.Web.RoutesSpec where
import Test.Hspec
import IHP.ControllerPrelude
import IHP.Test.Mocking
import qualified Data.Attoparsec.ByteString as Attoparsec
import Web.Types
import Web.FrontController
tests = beforeAll (mockContextNoDatabase WebApplication (pure ())) do
describe "Web.Routes" do
describe "DocumentationController" do
it "should be available at /docs" $ withContext do
"/docs/api-reference/0bca60db-571e-4cdd-b02a-8d5b9e7e6295" `shouldRouteTo` DocumentationAction { projectId = "0bca60db-571e-4cdd-b02a-8d5b9e7e6295" }
shouldRouteTo path action = (Attoparsec.parseOnly parseRoute' path) `shouldBe` (Right action)
如果您想在 IHP 应用程序中使用漂亮的 URL,您需要编写自己的 CanRoute 实例并解析路由字符串。
-- Web.Routes.hs
module Web.Routes where
import Generated.Types
import IHP.RouterPrelude
import Web.Types
instance CanRoute Co2ProducersController where
parseRoute' = do
string "/producers/"
let co2ProducerById = do
id <- parseId
endOfInput
pure ShowCo2ProducerAction {co2ProducerId = Just id, slug = Nothing}
co2ProducerBySlug = do
slug <- parseText
pure ShowCo2ProducerAction {co2ProducerId = Nothing, slug = Just slug}
co2ProducerBySlug <|> co2ProducerById
我将如何进行测试?我在这里看到了一个可以设置整个测试应用程序的示例:https://github.com/digitallyinduced/ihp/blob/2422bbdfa6165231e89b44c2e7d1c68b65f6b3b4/Test/RouterSupportSpec.hs
但我只想测试我是否正确解析路由,而不是发送请求和测试响应。
测试上述代码的简单方法是什么?
我们为自定义路由定义的 CanRoute
的 parseRoute'
函数基本上只是返回一个 attoparsec 解析器。
我们可以使用 attoparsecs parseOnly
到 运行 带有提供的输入字符串的解析器。这是一个例子:
module Test.Web.RoutesSpec where
import Test.Hspec
import IHP.ControllerPrelude
import IHP.Test.Mocking
import qualified Data.Attoparsec.ByteString as Attoparsec
import Web.Types
import Web.FrontController
tests = beforeAll (mockContextNoDatabase WebApplication (pure ())) do
describe "Web.Routes" do
describe "DocumentationController" do
it "should be available at /docs" $ withContext do
"/docs/api-reference/0bca60db-571e-4cdd-b02a-8d5b9e7e6295" `shouldRouteTo` DocumentationAction { projectId = "0bca60db-571e-4cdd-b02a-8d5b9e7e6295" }
shouldRouteTo path action = (Attoparsec.parseOnly parseRoute' path) `shouldBe` (Right action)