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.
Mock
object delegate to a real object for some operations (although doing so is discouraged). 2. In your particular case, perhaps you could mockHttpClient
instead withHttpOverrides
? – Subtropicsthis.firstMethod()
it is bound to the object and automatically called when the first one is called. Replacing the call would only work with DI passing a newthis
. Or am I missing something? While HttpOverrides are useful in general, I don't see how it can help me mitigate executing the code from the first method again. I would like to stick to one test for one function. – Kickstand