1.
Why do we write unit tests?
Correct Answer
D. To see whether the given module works.
Explanation
Unit tests are written to verify the functionality and correctness of a specific module or component of a software application. By writing unit tests, developers can ensure that the module performs as expected and meets the desired requirements. It helps in identifying any issues or bugs in the module, allowing for early detection and resolution. Unit tests also promote code quality and maintainability by providing a safety net during future code changes or updates. Ultimately, unit tests help in building reliable and robust software systems.
2.
Which is not a par of test's structure?
Correct Answer
C. Playback
Explanation
Playback is not a part of the test's structure because it refers to the act of executing the test case and observing the results. The structure of a test typically includes asserting, which involves checking whether the actual results match the expected results, test setup, which involves preparing the necessary conditions for the test, and calling the tested method, which is the specific action being tested. Playback, on the other hand, is the step where the test case is executed and the expected results are observed.
3.
How many concerns should each unit test?
Correct Answer
D. One
Explanation
Each unit test should focus on testing one specific concern or aspect of the code. This allows for better isolation of issues and makes it easier to identify and fix bugs. By testing only one concern per unit test, it becomes clearer where any failures or errors are originating from, making the debugging process more efficient. Additionally, having one concern per unit test promotes better code organization and maintainability.
4.
Which test runner should you use?
Correct Answer
D. Mocha
Explanation
Mocha is the recommended test runner to use. It is a popular JavaScript test framework that provides a simple and flexible way to write tests for Node.js and the browser. Mocha offers a wide range of features such as support for asynchronous testing, customizable reporting, and easy integration with other libraries. It has a large and active community, making it a reliable choice for running tests in JavaScript projects.
5.
Which assertion library should you use?
Correct Answer
A. Chai
Explanation
Chai is the correct answer because it is a popular and widely used assertion library in JavaScript testing. It provides a variety of assertion styles and supports both BDD and TDD syntax. Chai offers a clean and expressive way to write assertions, making it easier to test and verify the expected behavior of code. It integrates well with different testing frameworks and provides useful features like deep object comparison and assertion chaining.
6.
What should you use as a test spy, stub and mock?
Correct Answer
C. Sinon
Explanation
Sinon is a JavaScript library that can be used as a test spy, stub, and mock. It provides functionalities to create fake functions and objects for testing purposes. With Sinon, you can spy on function calls, stub function behavior, and create mock objects with predefined behaviors. It is a popular choice for testing in JavaScript and allows for easy verification and manipulation of function calls during tests.
7.
What do test spies tell you?
Correct Answer
C. How many times function calls were called
Explanation
Test spies are used to track and record information about function calls during testing. They provide insights into how many times a specific function was called, allowing developers to verify if the function is being invoked as expected. By using test spies, developers can ensure that the correct number of function calls are being made, helping to identify any potential issues or bugs in the code.
8.
What is a mock?
Correct Answer
B. A fake method with a pre-programmed behavior and expectations
Explanation
A mock is a fake method with a pre-programmed behavior and expectations. This means that it is a simulated version of a method that is used for testing purposes. It allows developers to mimic the behavior of a real method without actually executing the code within it. This is useful for testing and debugging, as it allows developers to isolate and verify the functionality of specific parts of their code. By using a mock, developers can control the inputs and outputs of the method, making it easier to test different scenarios and ensure the overall quality of their code.
9.
Which report coverage on metrics isn't real?
Correct Answer
B. pHrase
Explanation
The correct answer is "phrase". In the context of report coverage on metrics, a phrase does not provide real coverage as it is a group of words that may not necessarily convey any meaningful or measurable information. On the other hand, a line, statement, or branch can all represent specific elements or aspects of a report that can be measured and analyzed for coverage on metrics.
10.
Which statement about stubs isn't true?
Correct Answer
D. You must use it in every step
Explanation
The statement that is not true about stubs is that "You must use it in every step". Stubs are used in software testing to simulate the behavior of components that a module being tested depends on. They are used to replace the target function and control its behavior, as well as to prevent calls to external resources. However, stubs are not required to be used in every step of the testing process. They are selectively used when needed to isolate and test specific components.