959eb576aa
Fixes: #15, #13, #14, #11, #8 Add the IDEAS to a 'Suggestions' section in the FUTURE_PLANS file. * Updated the cabal instructions for level 03 * Removed the duplicated config loading in level 05 * Add the implementations for File.hs for levels 06 & 07 * Renamed the slightly misleading 'readObject' function * Fixed the capitalisation of init and close DB functions between levels 04 & 05. |
||
---|---|---|
.. | ||
bin | ||
src/FirstApp | ||
tests | ||
changelog.md | ||
default.nix | ||
level03.cabal | ||
level03.nix | ||
LICENCE | ||
README.md | ||
Setup.hs | ||
stack.yaml | ||
test.json |
Level 03
In this exercise we're going to add some tests to our application. Because types are awesome, and tests are pretty good. But types AND tests is pretty much perfect.
These tests will not be awe inspiring, this exercise is primarily to introduce you to adding tests to your Haskell application. The setup of the Cabal file is already completed for you, but will be covered.
As is to be expected, there are multiple testing frameworks and packages available but we will only cover one here. We will use the HSpec framework, with the hspec-wai package to make our lives a bit easier.
NB: Including Test Library Dependencies
For a cabal sandbox:
$ cabal sandbox init
$ cabal install --only-dependencies --enable-tests
$ cabal configure --enable-tests
For a stack environment:
$ stack build --test
Start in tests/Test.hs
.
Aside: Tool Introduction - ghcid
Additionally we'd like to introduce a command line tool that you may find useful for Haskell development; ghcid. This is a very lightweight tool that works for any project with a functioning cabal setup.
If you would like to use it, consult its documentation for how to install it,
and then in an spare open terminal window, navigate to the root of the Haskell
project and run $ ghcid
.
It will then attempt to build your project, if errors are found they will be
displayed. But more importantly you can go back to editing files in the project
and ghcid
will refresh in the background. Providing you with new error
messages or All Good
if it cannot find any errors.
ghcid
provides extremely fast feedback, allowing for a nice development
process with constant feedback about your changes. It is very useful in tandem
with type holes. Give it a try!
ghcid
can also help out when you're writing your tests. Since normally the
source and other packages that are listed in a test-suite
configuration
section in the Cabal file are not loaded. You can manually tell ghcid
to
load and examine these files with the following command:
$ ghcid -c "cabal repl level03"
# Or for using ghcid to check your tests
$ ghcid -c "cabal repl level03-tests"
# A note for sandboxed ghcid, you may need to provide an
# explicit path to the binary:
$ .cabal-sandbox/bin/ghcid -c "cabal repl level03-tests"
It should work with stack
as well:
$ ghcid -c "stack repl level03-tests"
Please read the ghcid
documentation for installation and usage
instructions. The FAQ provides
some useful tips.