How to Test Rails App using Mocha JS and Chai JS?
We were amused by the syntax provided by konacha gem.
This is very similar to our RSpec syntax. And since we were familiar to rspecs we decided to choose konacha gem as our alternative. So why to wait, let us start integrating this with our rails app.
How to add?
We just need to add it to our gemfile
group :test, :development do gem 'konacha' end
$ bundle install in your command line. And we are good to go!
How it can be used?
Now we need to create a directory named
spec folder. The following command will take care of it!
Any test files that we add in this folder must have suffix as
Note: We can change the above suffix in the
config/initializers/konacha.rbbut I advise not to change that configuration and try to keep things simple 🙂
Life is really simple, but we insist on making it complicated… – Confucius
In above code
it block are analogous to RSpec blocks. Mocha JS provides
afterEach() blocks. Chai JS provides
The very first line
//= require calculator in our
calculator_spec.js adds the
Everything is ready to test. Perfect! Lets see them running!
We can run them either in console (command line) or in browser itself. Lets have a look at both these methods.
How to run tests in browser?
Just a simple command and you are good to go!
$ bundle exec rake konacha:serve
Now we can see our test running at http://localhost:3500 . It will look like below.
How to run tests from command line?
Now for doing this step we need to initialise few things. Create a file
config/initializers/konacha.rb and paste in below contents.
Here we tell konacha that spec directory is
_test in their filenames. We also need to specify web driver such as
:poltergeist so it can run the test headlessly (without GUI or console based).
Further we need to run the below command.
$ bundle exec rake konacha:run
You will see results like below.
Above screenshot shows there is one failed test and the rests are passed!
We can also run individual specs by below command.
$ bundle exec rake konacha:run SPEC=individual_spec_file_path
How can I test views?
Lets convert our
erb template into
ejs template just by adding static values for testing purpose.
First of all we need to add the gem
ejs into our gemfile.
group :development, :test do gem "ejs" end
As a best practice we must organise our templates at one common place. Lets create directory
To make this available for our tests we must add following contents to file
//= require_tree ./templates
This will make all the templates available for testing!
Now lets add the tests at
In the first two lines we say add
It compiles the JST template to HTML view. And we replace the body with this JST view so we can mimic what we want to test. After the view is created we can just write our expectations and assertions.
Now just run your tests they must pass now 🙂 That’s it!
– Ankur Vyas (@ankurvyas27) July 2, 2017
Thanks for reading. If you liked it, press the heart button 🙂
At BoTree we help individuals, startups and businesses build their ideas from Canvas to Cloud. We fix bugs for free, forever. We can help you with Support and Maintenance of existing applications not developed by us. We also provide 24 x 7 support to all our clients.
At BoTree Technologies, we build enterprise applications with our RoR team of 25+ engineers.
Consulting is free – let us help you grow!
Choose Your Language
- Digital Marketing
- IT Consulting
- Project Management
- Salesforce Development