I don't know that setting up a Grunt task to run your Mocha tests is the best solution. The Grunt tasks in the Sails "pipeline" are run every time you do sails lift
; running your tests every time you lift the server could become very annoying as you add more tests to your suite, because you tend to do sails lift
a lot during development!
You certainly can run the tests manually; you just need to install Mocha globally:
sudo npm install -g mocha
Then in your app's root directory, simply type mocha
to run all the tests under /test
. You can specify a subset of tests to run by giving the command a subdirectory, e.g. mocha test/controller
.
As far as the tests themselves are concerned, it's possible to unit test a controller by requiring it the way you're doing, but it means you'll have to mock up request and response objects. A better way is to lift a Sails instance in your before
function, and then make HTTP requests to your controllers. See answers to this question for a couple of examples of lifting Sails from within tests, either before every test or before each test. You can then use the Node http
module to make requests to your controllers, or make life easier on yourself and use the request module.