Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organising tests using Mocha & Should.js

I'm new to unit testing using the Mocha & should.js. I'm using the Mocha's BDD for testing my application. The application I'm testing has different components like account, products & order. Before moving the code to the git repository I want to test all aspects of the application. I've different test files for all the components. For example, account.js for account, order.js for order & so on.

I want to test all the components against a temporary test account. So the flow is:

  1. Create test account
  2. Test all functionality related to account (update profile, change password etc)
  3. Test all functionality of the account's product
  4. Test all functionality of the account's order
  5. Delete the test account & all info related to it

My question is how do I make sure the temporary account is created before executing other tests?

Since I've test cases in different file how do I make sure they are executed in the same order as mentioned above? Is there any other better way to test the application?

Thanks.

like image 686
sunilkumarba Avatar asked Jun 05 '13 07:06

sunilkumarba


1 Answers

Your unit tests should be independent: execution of one should not affect execution of the others. Using Mocha, I do this by having every test file require a utilities file with a beforeEach function that will clear the database, establish connections, etc., for every unit test. There is also an afterEach function that cleans up and disconnects after every unit test.

If every single unit test that you will run requires a test account, you can set the test account in a similar top-level beforeEach function. Otherwise, you can set up the test account in a beforeEach function within the desired Mocha describe blocks.

like image 128
Jorge Aranda Avatar answered Oct 21 '22 11:10

Jorge Aranda