Testing Yesod Handlers - Examples

I spent several hours trying to get a basic test environment to test Yesod handlers. I still have problems. It would be helpful to see a complete working example that includes interacting with the database.

Can anyone please point me to any example on the internet? The yesod project is ideally open source, so I can see the entire scaffold. I've found a few bits and pieces from various sources, but they haven't helped me yet.

+3


source to share


1 answer


I don't think they are particularly good test cases (there is a lot of repetition and the database is not automatically erased between test cases), but you can take a look at these specs I made for this site . The entire open source project on Github .

Here's an example:



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"

      

+1


source







All Articles