I am currently trying to develop test-driven with Flutter and Dart. I have an object that has two methods of which the first one does an http call and the second one calls the first method. In order to test the first function I mock the dependencies of the that function (namely the http call).
Now I want to test the second method, but I was not able to find a way to mock only the first function while keeping the rest of the object intact. As a result I can only mock the dependencies of the first method again which results in the entire function being executed all over. This goes against the whole purpose of unit testing.
It seems like there is only an all or nothing approach when it comes to mocking objects. I wonder how one is to go about a case where some object is reliant on methods on the same object.
Using Fake
and Mock
does not allow me to call the original method. spy
is deprecated and assigning a mock function to one of the functions does not work since Dart does not allow me to reassign a method.
I found a way to solve my problem. According to this link mentioned in the comments "[t]esting with real objects is preferred over testing with mocks". In my case I just subclassed the main object I was testing and replaced the (second-)method that I was not testing. This seems to do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With