Jasmine FAQ (Unit Testing) - devuxd/SeeCodeRun GitHub Wiki
The tests will be designed and implemented based on the requirements and functions of the software application. Using a behavior-driven approach that is more business value perspective to ensure the software comply with the intended usage (validation) and it fulfills the requirements (verification). This type of development also emphasizes on writing unit tests and from outside-in (focus on the goal of a piece of code). Some area of testing that can be done including user inputs, database integrity, and source code reachability using different sets of coverage criterion. These are also things that should be tested to ensure all the test requirements are satisfied, which are derived based on the functionality of this software.
Individual developers will be responsible for ensuring the correctness and robustness of the code they write and develop Unit tests for possibly each use case scenario of the system.
Jasmine provides an organized and consistent way inside each describe() function using the beforeEach() function to setup state before running each test and afterEach() function to teardown the remaining state after running each test inside a spec. These two functions are not required to have inside the describe() function, however, it is definitely important to put the system under the state that is necessary to run a test correctly.
describe(name,function) - a function for grouping related specs with a given name.
Most of the behavior that I want to test depends on data loaded from Firebase in the cloud. How do I test this?
Jasmine provides the function of setting the states before each test using beforeEach(). A tester can load the necessary data from Firebase by creating a reference to the firebase database using the firebase api and pass in the URL to the firebase app. After retrieving the data, they can be set to the current session variables for further testing purpose.
I'm writing some functionality in a new JS file. Is there a convention on where I should put the corresponding tests in the file structure?
There isn't any restriction or special conventions of where the code should be placed with the corresponding tests. Generally in other unit testing frameworks and languages (ex. JUnit in Eclipse), all the unit tests are stored together in a same class and created in separate functions. Similarly, Jasmine can group similar test together using "a test suit begins with a call to the global Jasmine function describe with two parameters: a string and a function", and the convention is to name the suite with what functionality or code is under testing [2].
One way to run your test is to first download the standalone distribution which contains all the libraries you need to start running Jasmine. Unzip the file and open SpecRunner.html will allow you to start running the default/included specs (test suites). To run your own customized test, you simply replace the link for the respective specs inside the head section of the SpecRunner.html with your own customized specs.
A spy is a function that is used for creating test doubles. It can be used to "stub any function and tracks calls to it and all arguments. A spy only exists in the describe or it block in which it is defined, and will be removed after each spect[2]." Generally, a test double is used whenever you try to test the system under conditions that might cause a permanent effect on the current system, therefore you create a "double" to protect your current system from the after effect.
Below are sources where I referenced most of the information for creating this wiki page. You will be able to learn most of important usage related details through the link of Jasmine Introduction.
- Jasmine Background (http://www.htmlgoodies.com/beyond/javascript/testing-javascript-using-the-jasmine-framework.html)
- Jasmine Introduction (http://jasmine.github.io/2.0/introduction.html)
- Jasmine on Github (https://github.com/jasmine/jasmine)
- Jasmine Release (https://github.com/jasmine/jasmine/releases)
- Jasmine Unit Test (https://www.adobe.com/devnet/archive/html5/articles/unit-test-javascript-applications-with-jasmine.html)