如果您想在 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/digitallyducing/ihp/blob/2422bbdfa6165231e8??9b44c2e7d1c68b65f6b3b4/Test/RouterSupportSpec.hs
但我只想測驗我是否正確決議路由而不發送請求并測驗回應。
測驗上述代碼的簡單方法是什么?
uj5u.com熱心網友回復:
我們為自定義路由定義的parseRoute'函式基本上只是回傳一個 attoparsec 決議器。CanRoute
我們可以使用attoparsecsparseOnly使用提供的輸入字串運行決議器。這是一個例子:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/423022.html
標籤:
下一篇:從例外字典中提取vale
