1

I'm working on a scala object in order to perform some testing

My start object is as follows

object obj1 {
def readvalue : IO[Float] = IO{
scala.io.StdIn.readFloat()
}
} 

The testing should be 1- value of type FLOAT 2- should be less than 3

As we can not mock singleton objects I've used mocking functions here is what I've done.

class FileUtilitiesSpec
    extends FlatSpec
    with Matchers
    with MockFactory
    {

  "value" should "be of Type Float" in {
    val alpha = mockFunction[() => IO[Float]]
    alpha.expects shouldBe a[IO[Float]]

  }
  "it" should "be less than 3" in {
    val alpha = mockFunction[() => IO[Float]]
    alpha.expects shouldBe <(3)

  }

} 

Im getting an error saying that :


 MockFunction0-1() once (never called - UNSATISFIED) was not an instance of cats.effect.IO, but an instance of org.scalamock.handlers.CallHandler0
ScalaTestFailureLocation: util.FileUtilitiesSpec at (FileUtilitiesSpec.scala:16)
Expected :cats.effect.IO
Actual   :org.scalamock.handlers.CallHandler0 ```


Her sincerly
  • 373
  • 1
  • 13
  • You are not calling any implementation in this example, so the mock function is never called. Or is this example incomplete? Then please add the rest. The second test (<3) also won't work as the return value for your mock has not been define anywhere. The default value will be a `null` as your type is a reference type. – Philipp Apr 14 '20 at 12:06
  • Thats the complete example !! I m new to all of this , it's my first test so bare with me please . I dont know what implementation I should do – Her sincerly Apr 14 '20 at 12:23

1 Answers1

0

I would recommend reading the examples here as a starting point: https://scalamock.org/quick-start/

Using mock objects only makes sense if you are planning to use them in some other code, e.g. dependencies you do not want to make part of your module under test, or are beyond your control. An example might be a database connection where you would depend on an actual system, making the code hard to test without simulating it.

The example you provided only has mocks, but no code using it, hence the error you are getting is absolutely correct. The mock expects to be used, but was not called. The desired behaviour for a mocking library in this case is to make the test fail as the developer intended for this interaction with a mock to happen, but it was not recorded - so something is wrong.

Philipp
  • 967
  • 6
  • 16