metamaps--metamaps/doc/RspecTesting.md

58 lines
2.2 KiB
Markdown
Raw Permalink Normal View History

2016-04-26 02:22:32 +00:00
## Testing Javascript
Javascript tests are under construction, but you can read more in the README in `app/assets/javascripts`.
2015-12-17 01:34:34 +00:00
## Testing with RSpec
2016-01-07 07:32:11 +00:00
RSpec is a ruby gem that allows you to test your code base. This is great -
every time you make a change, you can do some basic sanity checks to make sure
you didn't break anything.
2015-12-17 01:34:34 +00:00
2016-01-07 08:06:35 +00:00
To test Metamaps, run
rspec
in the top level directory. It will automatically search the `spec` directory
for files called `*_spec.rb`, and run them as tests. When it's done testing, it
will print a report telling you how many tests failed. With luck, the number
will be 0.
2015-12-17 01:34:34 +00:00
2016-01-07 07:32:11 +00:00
Note that if your test database doesn't exist yet, you'll need to create it
first:
2015-12-17 01:34:34 +00:00
2016-01-07 07:32:11 +00:00
RAILS_ENV=test rake db:create
At the time of writing, there are four directories in the spec folder. One,
`support`, is for helper functions. `rails_helper.rb` and `spec_helper.rb` are
also for helper functions.
2018-03-10 16:10:09 +00:00
`factories` is for a gem called [factory-bot][factory_bot]. This gem lets you
2016-01-07 07:32:11 +00:00
use the `create` and `build` functions to quickly create the simplest possible
valid version of a given model. For instance:
2015-12-17 01:34:34 +00:00
let(:map1) { create :map }
2018-03-10 16:10:09 +00:00
let(:alex) { create :user, name: "Alex" }
let(:map2) { create :map, user: alex }
2015-12-17 01:34:34 +00:00
2016-01-07 07:32:11 +00:00
As you can see, you can also customize the factories. You can read the full
documentation at the link above or check the existing specs to see how it works.
It is worth reading through the factories to see how they are defined. If you
add a model to `app/models`, please also create a factory for it that defines
the minimum valid state for that model.
2015-12-17 01:34:34 +00:00
2016-01-07 07:32:11 +00:00
Finally, `models` and `controllers` have the actual spec files. Writing specs is
usually fairly simple but you do need to understand the syntax. You can read
more at [rspec.info][rspec-docs].
2015-12-17 01:34:34 +00:00
2016-01-07 07:32:11 +00:00
If you modify the metamaps codebase, please consider adding tests verifying that
the added code works. This will help in a few ways:
2015-12-17 01:34:34 +00:00
- Unrelated changes in the future that break your code will be spotted earlier
2016-01-07 07:32:11 +00:00
- Your changes will be more easily understood, since the *purpose* will be
described by the spec
2015-12-17 01:34:34 +00:00
Happy testing!
2018-03-10 16:10:09 +00:00
[factory-bot]: https://github.com/thoughtbot/factory_bot
2015-12-17 01:34:34 +00:00
[rspec-docs]: http://rspec.info