These are chat archives for etorreborre/specs2

Jun 2018
Vamsi Ampolu
Jun 16 2018 07:00

Hey @etorreborre thanks for your comment on my question, I have updated it just now, a few thoughts struck me when I was looking at the question again:

1) I have asked the specs2 framework to mock Interactions and return a mock of the object

2) I have NOT ASKED/SPECIFIED my code to replace Interactions with MockInteractions.

2.5) I am using a singleton which is apparently not good for testing, let's assume I have changed that to a class

3) This is a deeper question of switching out implementation using either an IoC container (IDK much about that in Java, let alone Scala) or doing something like:


new (ProvisoBankStatementsHandler with MockInteractions).handler(???)

As an apology for the above line of thought (and as a defence) I have to say that I have been reading a Groovy book where they have a non-intrusive way of applying traits.

Vamsi Ampolu
Jun 16 2018 07:18

This question here suggests that you override the method manually to call the mock:

instead of the real code, if I override the specific method to call the mock and verify that the mock is called when I call the method, would it still be unit testing. I'm not sure but shouldn't I use the trait as if it were an interface, should I create a seperate InteractionsInterface trait and mock it, extend the class with that one and then provide a Real and Fake for implementation and test respectively:

trait InteractionsInterface {
  def authenticateUser(token: String): Either[Errors, Boolean]
// Implementation

class Interactions extends InteractionsInterface {}

class MyHandlerImpl extends MyBaseHandler with Interactions {
   def self: InteractionsInterface

// Test
class MyHandlerTest extends MyBaseHandler with MockInteractions {
   def self: InteractionsInterface

Then we check that handler is calling authenticateUser

If I extend other traits such as a Utils, IDK how this approach might work, IDK how to structure my code for testability but would this work, is there any IoC container or something that might help me accomplish this?