testingyesodpersist

Testing Yesod handlers - examples


I've spent several hours trying to get a basic test framework going for testing Yesod handlers. I'm still having some trouble. It would be useful to look at a full working example, which includes interaction with the database.

Can someone direct me to any example(s) on the web? Ideally an open source yesod project, so I can see the scaffold in its entirety. I've found several bits and pieces from different sources but so far they have not helped me all that much.


Solution

  • I don't think they're particularly great test cases (there's alot of repetition and the database isn't wiped automatically between test cases), but you can take look at these specs I made for this website. The whole project's open source on Github.

    Here's a sample:

    homeSpecs :: Spec
    homeSpecs =
        ydescribe "These are some example tests" $ do
    
            yit "loads the index and checks it looks right" $ do
                _ <- runDB $ rawExecute "TRUNCATE TABLE hack_day, project;" []
                get HackDayR
                statusIs 200
                htmlAnyContain "h2" "New Hackday"
    
            yit "shows the current hackday" $ do
                _ <- runDB $ rawExecute "TRUNCATE TABLE hack_day, project;" []
                currentTime <- liftIO $ getCurrentTime
                _ <- runDB $ insert $ HackDay { hackDayTitle = "testTitle"
                                              , hackDayCreated = currentTime
                                              , hackDayVotingClosed = False }
                get HackDayR
                htmlAllContain ".currentHackday" "testTitle"