具有以下Scotty端点:
myendpoint :: Text -> ScottyM ()
myendpoint info =
post "/foo/bar/:var" $ do
var :: Text <- param "var"
query :: Query <- jsonData
result <- liftIO $ retrieveResult info var query
json $ result
runApi :: IO ()
runApi = scotty 4602 $ do
myendpoint例如,如何使用不同的可能输入测试myendpoint
info1 = "foo"
query1 = Query { qParam1 = "foo", qParam2 = "bar" }
var1 = "bar"
expect1 = Result { foo = "foo", bar = "bar" }
res1 = decode $ test_myendpoint info1 query1 var1
res1 `shouldBe` expect1
info2 = "baz"
query2 = Query { qParam1 = "hello", qParam2 = "there" }
var2 = "boo"
expect2 = Result { foo = "biz", bar = "dev" }
res2 = decode $ test_myendpoint info2 query2 var2
res2 `shouldBe` expect2我很想简单地在另一个线程中运行API,然后用一些JSON真正地查询端点,但是有没有一种更干净的方法呢?(虽然上面的好处是真正的端到端测试)
发布于 2021-05-14 04:57:23
找到答案了。需要导入hspec-wai和hspec-wai-json。
import Network.Wai (Application)
import Network.Wai.Handler.Warp (run)
api :: IO Application
api = scottyApp $ do
myendpoint
runApi :: IO ()
runApi =
api >>= run 460现在我们可以在测试中自由使用with api了:
import Test.Hspec
import Test.Hspec.Wai as W
import Test.Hspec.Wai.JSON
spec :: Spec
spec = with api $ do
describe "testing some endpoint" $ do
it "should return correct data" $ do
get "/foo/bar/whatever" `shouldRespondWith`
[json|{foo: "foo", bar: "bar"}|]https://stackoverflow.com/questions/67520834
复制相似问题