Are you testing methods that simply change the state of an object?

I have an object on which there is a public void method that only modifies the internal state. Part of me feels as if she should have tests, as this is a public method, but how are you going to test this? Are you worried or rely on other tests that use the fact that the internal state has changed?

To add an example, I implement an intermediate buffer that has an insert method on it and a moveCursorForward method that only modifies the internal state.

Thanks Aly

+4
source share
2 answers

I prefer separate tests for such methods. For example, there are two cases for the moveCursorForward method: 1. the cursor is already at the end of the buffer 2. the cursor is not at the end of the buffer

So, it is likely that case 1 will be skipped if you do not create a special test for it. In other words, you can skip some boundary cases.

+2
source

I think injection is your answer. Create a Cursor object and enter it in Buffer . Unit test cursor object so you know that it works great;). Since the cursor is consistent, and you passed it to your buffer, you can assert it in the cursor object when testing moveCursorForward .

Check Out Michael Feather Works Effectively With Legacy Code

It can also be useful for mocking objects.

+3
source

Source: https://habr.com/ru/post/1385187/


All Articles