1

I am building an app with locomotive.js, and I am looking to build my test suite using the Mocha test framework. I am also new to TDD/BDD in general, so please take that into consideration. I am curious if anyone can point me in a good direction to start testing a locomotive based app.

My biggest question would be:

  1. How do I test a controller's actions?
  2. Can I test an initializer?
  3. Are there best practices around creating test request objects?
Adam Duro
  • 882
  • 1
  • 9
  • 21

1 Answers1

2
  1. Testing controller actions
    It depends on what exactly you want to test. Controller actions usually either return content, or pass (perhaps in case of errors) the request along the server stack, so testing them means using something like supertest to check for the correct responses (the supertest page also mentions how to use it together with Mocha)

  2. Can I test an initializer?
    Testing an initializer by itself is difficult, because they require to be run within the context of a LocomotiveJS application. However, you could create a test just booting up the application, which during the booting process will also run all initializers. I just added a simple Mocha-based testing framework to my Locomotive + Sequelize boilerplate project which shows how to boot the Locomotive app from within Mocha.

  3. Are there best practices around creating test request objects?
    If you mean how you can check responses to requests, look at the aforementioned supertest or perhaps mocha-headless.

robertklep
  • 198,204
  • 35
  • 394
  • 381
  • Thanks! Much of this I had already figured out on my own, but I'll gladly let you take the cred for the answer. Well done. – Adam Duro Jan 10 '13 at 08:39
  • @AdamDuro- I know this has been awhile, but how did you test controllers? – rgin Jul 02 '15 at 00:10