B
B
bogushevich2013-02-18 05:20:07
Unit testing
bogushevich, 2013-02-18 05:20:07

Choosing an isolation level for each module

Hello.
I'm currently learning unit testing and I have a question about the correct isolation level for each module.
Let's imagine that we have two classes in JS that together represent the same component or layer:

function HighClass() {
   this.lowClass = new lowClass();
}
function LowClass() {
   this.db = new SomeDB.Connecton();
}


I see two ways of testing:
1. Since both classes are logically related, we will test them together. As an external dependency, I choose the database connection (new SomeDB.Connecton()) and replace it with a stub.
2. I test each each class separately. I provide MockLowClass as a stub for HighClass and MockDB for LowClass.

Which way is correct if we are talking specifically about unit testing? When unit testing, do we always have to choose one class as the code under tests, or can a component consisting of several classes?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
EugeneOZ, 2013-02-18
@EugeneOZ

Second option. As a bonus, get a code that adheres to the Single Responsibility Principle more strictly.

F
funca, 2013-02-23
@funca

correctness, like any evaluative concept, depends on the context. in your case - from the purposes of testing. unit testing and unit testing are not the same thing. :)
with the help of unit testing, the correctness of the application elements is checked separately. it is important that the tested aspects do not overlap (the same thing is not tested by different tests and the tests do not fall if something breaks in a completely different place). logically, this is possible only at the level of atomic elements, which in OOP are classes and methods. and all that is higher will inevitably give correlations associated with the system. for example, if a LowClass implementation breaks, it should not affect the HighClass unit tests—the LowClass unit tests will report such a break to you. so the correct strategy would be 2.
integration tests are used to test the "assembly" system. they are also very necessary - after all, it is important not only to assemble from proven parts, but also to connect them correctly - here you will use strategy 1.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question