Update the example in the yesod-test README

* Fixes a few typos/formatting issues as well
* Closes #920
This commit is contained in:
Maximilian Tagher 2015-02-07 23:10:52 -08:00
parent 887d7e8004
commit b9246d26a1

View File

@ -2,55 +2,44 @@
Pragmatic integration tests for haskell web applications using WAI and optionally a database (Persistent). Pragmatic integration tests for haskell web applications using WAI and optionally a database (Persistent).
It's main goal is to encourage integration and system testing of web applications by making everything /easy to test/. Its main goal is to encourage integration and system testing of web applications by making everything *easy to test*.
Your tests are like browser sessions that keep track of cookies and the last Your tests are like browser sessions that keep track of cookies and the last
visited page. You can perform assertions on the content of HTML responses visited page. You can perform assertions on the content of HTML responses
using css selectors. using CSS selectors.
You can also easily build requests using forms present in the current page. You can also easily build requests using forms present in the current page.
This is very useful for testing web applications built in yesod for example, This is very useful for testing web applications built in yesod for example,
where your forms may have field names generated by the framework or a randomly where your forms may have field names generated by the framework or a randomly
generated "\_token" field. generated CSRF "\_token" field.
Your database is also directly available so you can use runDB to set up Your database is also directly available so you can use runDB to set up
backend pre-conditions, or to assert that your session is having the desired effect. backend pre-conditions, or to assert that your session is having the desired effect.
The testing facilities behind the scenes are HSpec (on top of HUnit). The testing facilities behind the scenes are HSpec (on top of HUnit).
This is the helloworld and kitchen sink. In this case for testing a yesod app. The code sample below covers the core concepts of yesod-test. Check out the
[yesod-scaffolding for usage in a complete application](https://github.com/yesodweb/yesod-scaffold/tree/postgres/test).
```haskell ```haskell
spec :: Spec
import Yesod spec = withApp $ do
import Yesod.Static
import qualified MySite.Settings as Settings
import MySite.Models
main :: IO a
main = do
cfg <- (loadConfig Test) >>= either fail return
st <- static Settings.staticDir
Settings.withConnectionPool (connStr cfg) $ \cnPool -> do
-- ... Perhaps some code here to truncate your test database?
app <- toWaiApp $ MyApp st cfg
runTests app cnPool $ mySuite
mySuite = do
describe "Basic navigation and assertions" $ do describe "Basic navigation and assertions" $ do
it "Gets a page that has a form, with auto generated fields and token" $ do it "Gets a page that has a form, with auto generated fields and token" $ do
get "url/of/page/with/form" -- Load a page get ("url/to/page/with/form" :: Text) -- Load a page.
statusIs 200 -- Assert the status was success statusIs 200 -- Assert the status was success.
bodyContains "Hello Person" -- Assert any part of the document contains some text. bodyContains "Hello Person" -- Assert any part of the document contains some text.
-- Perform a css queries and assertions. -- Perform CSS queries and assertions.
htmlCount "form .main" 1 -- It matches 1 element htmlCount "form .main" 1 -- It matches 1 element.
htmlAllContain "h1#mainTitle" "Sign Up Now!" -- All matches have some text htmlAllContain "h1#mainTitle" "Sign Up Now!" -- All matches have some text.
-- Performs the post using the current page to extract field values: -- Performs the POST using the current page to extract field values:
post "url/to/post/to" $ do request $ do
addToken -- Add the CSRF _token field with the currently shown value setMethod "POST"
setUrl SignupR
addToken -- Add the CSRF _token field with the currently shown value.
-- Lookup field by the text on the labels pointing to them. -- Lookup field by the text on the labels pointing to them.
byLabel "Email:" "gustavo@cerati.com" byLabel "Email:" "gustavo@cerati.com"
@ -58,19 +47,21 @@ This is the helloworld and kitchen sink. In this case for testing a yesod app.
byLabel "Confirm:" "secret" byLabel "Confirm:" "secret"
it "Sends another form, this one has a file" $ do it "Sends another form, this one has a file" $ do
post "url/to/post/file/to" $ do request $ do
-- You can add files this easy, you still have to provide the mime type manually though. setMethod "POST"
setUrl ("url/to/post/file/to" :: Text)
-- You can easily add files, though you still need to provide the MIME type for them.
addFile "file_field_name" "path/to/local/file" "image/jpeg" addFile "file_field_name" "path/to/local/file" "image/jpeg"
-- And of course you can add any field if you know it's name -- And of course you can add any field if you know its name.
byName "answer" "42" addPostParam "answer" "42"
statusIs 302 statusIs 302
describe "Db access, still very raw" $ do describe "Database access" $ do
it "selects the list" $ do it "selects the list" $ do
msgs <- testDB $ do (selectList [] [] :: SqlPersist IO [(Key SqlPersist Message, Message)]) -- See the Yesod scaffolding for the runDB implementation
assertEqual "One Message in the DB" 1 (DL.length msgs) msgs <- runDB $ selectList ([] :: [Filter Message]) []
assertEqual "One Message in the DB" 1 (length msgs)
``` ```