-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathPostsSpec.hs
64 lines (49 loc) · 2.08 KB
/
PostsSpec.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module Test.Controller.PostsSpec where
import Network.HTTP.Types.Status
import IHP.Prelude
import IHP.QueryBuilder (query)
import IHP.Test.Mocking
import IHP.Fetch
import IHP.FrameworkConfig
import IHP.HaskellSupport
import Test.Hspec
import Config
import Generated.Types
import Web.Routes
import Web.Types
import Web.Controller.Posts ()
import Web.FrontController ()
import Network.Wai
import IHP.ControllerPrelude
tests :: Spec
tests = aroundAll (withIHPApp WebApplication config) do
describe "PostsController" $ do
it "has no existing posts" $ withContext do
count <- query @Post
|> fetchCount
count `shouldBe` 0
it "calling NewPostAction will render a new form" $ withContext do
mockActionStatus NewPostAction `shouldReturn` status200
it "creates a new post" $ withParams [("title", "Post title"), ("body", "Body of post")] do
response <- callAction CreatePostAction
let (Just location) = (lookup "Location" (responseHeaders response))
location `shouldBe` "http://localhost:8000/Posts"
-- Only one post should exist.
count <- query @Post |> fetchCount
count `shouldBe` 1
-- Fetch the new post.
post <- query @Post |> fetchOne
post.title `shouldBe` "Post title"
post.body `shouldBe` "Body of post"
it "can show posts" $ withContext do
post <- newRecord @Post
|> set #title "Lorem Ipsum"
|> set #body "**Mark down**"
|> createRecord
response <- callAction ShowPostAction { postId = post.id }
response `responseStatusShouldBe` status200
response `responseBodyShouldContain` "Lorem Ipsum"
-- For debugging purposes you could do the following, to
-- see the HTML printed out on the terminal.
body <- responseBody response
putStrLn (cs body)