How Can I Effectively Test a Method That Alters Object State?

0
5
Asked By UserName42 On

I'm working on unit tests for a method in my class that indirectly modifies the object's state by calling another method. For example, in my code, I have a function that does something like this: `SomeClass::unit_under_test()` calls `this->f()`, which alters the state of the current object. My issue is how to properly test `unit_under_test` without redundantly testing `f()`, since I have a separate test for that. I can't mock `f()` either, as it would require altering the source code, and it might be too complex on its own. I'm looking for tips on the best ways to approach testing this kind of scenario without getting bogged down in the implementation details of `f()` or any functions it calls. What strategies do you suggest?

1 Answer

Answered By CuriousCoder88 On

You should test the outcomes based on the expected state changes, regardless of how those changes occurred. Just focus on what the state should look like after calling `unit_under_test()`. This means you can check whether `f()` did what it was supposed to before you dig into its own behaviour.

CodeNinja21 -

That sounds logical! If `f()` sets multiple values, would it be better to verify all of them or just one since there's a separate test covering `f()` thoroughly?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.