Assert on object at time of call #964
Replies: 1 comment 1 reply
-
This, as you have guessed, is the easy part and can be achieved the way you explained using
This is what gets me a bit puzzled: who is in charge of setting the state of If it is up to the client code (i.e., your code), you should have a You could test that batch states are being set correctly in a different test, but then again, one could say that it is not up to you to test the internal behaviors of a third party library. Does this make sense to you? |
Beta Was this translation helpful? Give feedback.
-
I've gone down a bit of a rabbit hole chasing this, so I think the easiest thing would be to describe my situation and what I'm hoping to accomplish.
Setup:
I have a class method with the signature
push(seq: Sequence<Data>)
whereData
is a data class that I control. Inside that method, the following occurs:Batch
object is created.Batch
is a 3rd party class outside of my control.Data
in the chunk, a number of setters are called on theBatch
object, with parameters derived from the properties of theData
object.send()
method is called on eachBatch
exactly once. This method sends the batch to an external resource.Because this
push
method sends data to an external resource, it doesn't return anything or alter any local state.Test:
I would like to verify the following:
Sequence<Data>
,Batch#send()
was called the correct number of times (equivalent to the length of the sequence divided by the chunk size, rounded up).send()
should also be mocked to a no-op, for obvious reasons.send()
theBatch
object it was called on is in the expected state.using
mockkConstructor(Batch::class)
andverify(exactly = numOfChunks){ anyConstructed<Batch>.send() }
would seem to be the path to satisfy 1, but I don't know how to satisfy 2. I don't know if there's a functionality to run assertions on objects as Mockk tracks calls being made on them.I suppose I could extract the
List<Data> -> Batch
functionality into a separate method, and test that through traditional means, but now that I've gone down this rabbit hole I'm curious if Mockk can achieve it all in one go as written.Thanks
Beta Was this translation helpful? Give feedback.
All reactions