Say I have the following:
@ImplementedBy(classOf[DefaultFoo])
trait Foo {
def a (s : String) : Int
}
class DefaultFoo @Inject()() extends Foo{
override def a (s : String) = 1
}
@ImplementedBy(classOf[DefaultBaz])
trait Baz {
def b (s : String) : Int
}
class DefaultBaz @Inject()(val f :Foo) extends Baz{
override def a (s : String) = 1
}
If I want to test, say DefaultBaz, I am normally using ScalaMock and I would mock as follows in my test spec:
class DefaultBazSpec extends AnyWordSpec with MockFactory{
val mockFoo = mock[Foo]
val b = new DefaultBaz(mockFoo)
// write tests
}
But I could also do this:
val mockFoo = mock[DefaultFoo]
Which one is better? Mock the trait or the default class implementation?
question from:
https://stackoverflow.com/questions/65890369/should-i-mock-the-trait-or-mock-the-class-using-scala-mock 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…