trait
NoArg extends DelayedInit with () ⇒ Unit
Value Members
-
final
def
!=(arg0: Any): Boolean
-
final
def
##(): Int
-
final
def
==(arg0: Any): Boolean
-
def
apply(): Unit
-
final
def
asInstanceOf[T0]: T0
-
def
clone(): AnyRef
-
final
def
delayedInit(body: ⇒ Unit): Unit
-
final
def
eq(arg0: AnyRef): Boolean
-
def
equals(arg0: Any): Boolean
-
def
finalize(): Unit
-
final
def
getClass(): Class[_]
-
def
hashCode(): Int
-
final
def
isInstanceOf[T0]: Boolean
-
final
def
ne(arg0: AnyRef): Boolean
-
final
def
notify(): Unit
-
final
def
notifyAll(): Unit
-
final
val
styleName: Int
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
-
def
toString(): String
-
final
def
wait(): Unit
-
final
def
wait(arg0: Long, arg1: Int): Unit
-
final
def
wait(arg0: Long): Unit
Inherited from () ⇒ Unit
Value Members
-
def
toString(): String
Inherited from AnyRef
Value Members
-
final
def
!=(arg0: Any): Boolean
-
final
def
##(): Int
-
final
def
==(arg0: Any): Boolean
-
def
clone(): AnyRef
-
final
def
eq(arg0: AnyRef): Boolean
-
def
equals(arg0: Any): Boolean
-
def
finalize(): Unit
-
final
def
getClass(): Class[_]
-
def
hashCode(): Int
-
final
def
ne(arg0: AnyRef): Boolean
-
final
def
notify(): Unit
-
final
def
notifyAll(): Unit
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
-
final
def
wait(): Unit
-
final
def
wait(arg0: Long, arg1: Int): Unit
-
final
def
wait(arg0: Long): Unit
Inherited from Any
Value Members
-
final
def
asInstanceOf[T0]: T0
-
final
def
isInstanceOf[T0]: Boolean
Ungrouped
-
final
def
!=(arg0: Any): Boolean
-
final
def
##(): Int
-
final
def
==(arg0: Any): Boolean
-
def
apply(): Unit
-
final
def
asInstanceOf[T0]: T0
-
def
clone(): AnyRef
-
final
def
delayedInit(body: ⇒ Unit): Unit
-
final
def
eq(arg0: AnyRef): Boolean
-
def
equals(arg0: Any): Boolean
-
def
finalize(): Unit
-
final
def
getClass(): Class[_]
-
def
hashCode(): Int
-
final
def
isInstanceOf[T0]: Boolean
-
final
def
ne(arg0: AnyRef): Boolean
-
final
def
notify(): Unit
-
final
def
notifyAll(): Unit
-
final
val
styleName: Int
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
-
def
toString(): String
-
final
def
wait(): Unit
-
final
def
wait(arg0: Long, arg1: Int): Unit
-
final
def
wait(arg0: Long): Unit
A function that takes no parameters (i.e., a
Function0
or "no-arg" function) and results inUnit
, which when invoked executes the body of the constructor of the class into which this trait is mixed.This trait extends
DelayedInit
and defines adelayedInit
method that saves the body of the constructor (passed todelayedInit
) for later execution whenapply
is invoked.This trait is somewhat magical and therefore may be challenging for your collegues to understand, so please use it as a last resort only when the simpler options described in the "shared fixtures" section of your chosen style trait won't do the job.
NoArg
is intended to address a specific use case that will likely be rare, and is unlikely to be useful outside of its intended use case, but it is quite handy for its intended use case (described in the next paragraph). One potential gotcha, for example, is that a subclass's constructor body could in theory be executed multiple times by simply invokingapply
multiple times. In the intended use case for this trait, however, the body will be executed only once.The intended use case for this method is (relatively rare) situations in which you want to extend a different instance of the same class for each test, with the body of the test inheriting the members of that class, and with code executed before and/or after the body of the test.
For example, Akka's
TestKit
class takes anActorSystem
, which must have a unique name. To run a suite of tests in parallel, each test must get its ownActorSystem
, to ensure the tests run in isolation. At the end of each test, theActorSystem
must be shutdown. WithNoArg
, you can achieve this by first defining a class that extendsTestKit
and mixes inNoArg
. Here's an example taken with permission from the book Akka Concurrency, by Derek Wyatt:Given this implementation of
ActorSys
, which will invokeshutdown
after the constructor code is executed, you can run each test in a suite in a subclass ofTestKit
, giving each test'sTestKit
anActorSystem
with a unique name, allowing you to safely run those tests in parallel. Here's an example from Akka Concurrency:UnitFixture
is used in this example, because in this case, thefixture.WordSpec
feature enabling tests to be defined as functions from fixture objects of typeFixtureParam
toUnit
is not being used. Rather, only the secondary feature that enables tests to be defined as functions from no parameters toUnit
is being used. This secondary feature is described in the second-to-last paragraph on the main Scaladoc documentation offixture.WordSpec
, which says:Since
FixtureParam
is unused in this use case, it could be anything. Making itUnit
will hopefully help readers more easily recognize that it is not being used.Note: As of Scala 2.11,
DelayedInit
(which is used byNoArg
) has been deprecated, to indicate it is buggy and should be avoided if possible. Those in charge of the Scala compiler and standard library have promised thatDelayedInit
will not be removed from Scala unless an alternate way to achieve the same goal is provided. Thus it should be safe to useNoArg
, but if you'd rather not you can achieve the same effect with a bit more boilerplate by extending (() => Unit
) instead ofNoArg
and placing your code in an explicitbody
method. Here's an example:Using this version of
ActorSys
will require an explicitbody
method in the tests: