Troubleshooting
A test leaks
Symptom: a warning that starts with Leak in and names a test, or an error when
Report Leaks as Errors is enabled. After the run, Understudy: ... leaked counts the
tests that leaked.
Cause: the test reached a non-deterministic member or wrote global state without declaring it, or ran for real an overload of a member it replaces.
Resolution: apply the fix the warning ends with: stub or fake the member, or declare it real with
Real. For more information, refer to Fix a leak.
A stub or fake has no effect
Symptom: the code under test still runs the real member, and the stub or fake records nothing.
Resolution: check the following:
- The code under test is in an assembly that is woven: not a package from the package cache, not excluded in the Project Settings, and not Burst code. For more information, refer to Woven code.
- The selection's instance and arguments match what the code under test passes. A captured variable covers one instance; use a lambda parameter for every instance.
- The selection names the overload the code under test calls, with the same type arguments: a selection of
Physics.Raycast(origin, direction, distance)doesn't match a call that also passes alayerMask, which is another overload. A leak warning that names another overload points to this. For more information, refer to Match arguments. - The access comes from the code under test, not from the test, which isn't woven: a test reads a fake through its
Value. - No declaration made after it covers the same accesses: the last one declared decides.
- The selection's instance isn't
nullwhen the code under test runs: a selection whose instance chain holdsnullmatches nothing, without a warning. - The Console window shows no warning that the selection
matches nothing. That warning means evaluating the selection threw, or its weaving is incomplete.
Raise doesn't reach the handlers of the code under test
Symptom: at a Raise, a warning that the event still holds handlers on the real
event.
Cause: the code under test subscribed before the event fake existed, so those handlers stayed on the real
event, where the fake's Raise doesn't reach them.
Resolution: declare the event fake before the code subscribes, for example before creating the
object that subscribes in its Awake. A subscription made from [OneTimeSetUp] is discarded before
the first test: subscribe in [SetUp]. For more information, refer to
Fake an event.
A spy, stub or fake throws outside a test
Symptom: creating a spy, stub or fake throws InvalidOperationException, or using one
throws with a message that names a finished test.
Cause: a double belongs to the NUnit test or fixture that created it. It can't be created outside one, and it ends with it.
Resolution: create it in the test, in [SetUp] or in [OneTimeSetUp]. For more
information, refer to Lifetime.
This selection is not woven
Symptom: creating a double throws InvalidOperationException with
This selection is not woven.
Cause: Understudy reads a selection when Unity compiles a test assembly, and only from a lambda written in the call to the entry point. A delegate held in a variable, an assembly compiled outside Unity, or a test assembly whose weaving failed has no woven selection.
Resolution: write the lambda in the call, and run the test from the Test Runner. If the Console
window also shows Understudy: nothing is woven or Understudy: weaving, fix that first.
A declaration never decides an access
Symptom: a warning that a stub, fake or Real never decides an access, or that an answer
never runs.
Cause: the last declaration decides an access. A broader declaration made later covers every access of the earlier one, so the earlier one never decides.
Resolution: declare the broad one first, then the narrow one. For more information, refer to Stub some calls and not others.
A write from OneTimeSetUp is discarded
Symptom: a warning, before a fixture's first test, that a write into a fixture's double is gone.
Cause: code run from [OneTimeSetUp] wrote into a double declared for the fixture, such as
an object subscribing to its event fake, and every test starts that double fresh.
Resolution: run that code in [SetUp], which runs before each test. For more information,
refer to Every test starts fresh.
A domain reload during this test run
Symptom: the warning Understudy: a domain reload during this test run ended every spy, stub
and fake. Using a double afterwards throws A domain reload ended this spy, stub or fake.
Cause: a test entered Play mode with domain reload on. A reload ends every double, and a test's locals don't survive it.
Resolution: create the doubles again after the reload, or go to Edit > Project Settings > Editor and, under Enter Play Mode Settings, set When entering Play Mode to Reload Scene only. For more information, refer to Domain reloads.
An address held across statements
Symptom: a warning that an address held across statements reaches a member directly.
Cause: a ref local, a ref return or a pointer holds the address of a stubbed
field or property. An address can't run a stub's Returns or Do, so those accesses reach the
real member.
Resolution: fake the member instead, which gives the address a stored value, or change the code to use the member in place.
A member is also reached from code Burst compiles
Symptom: a warning that a member is also reached from code Burst compiles.
Cause: Burst code isn't woven, so the calls the warning lists run for real and aren't recorded.
Resolution: if the test depends on those calls, test that code another way, or move the call out of the Burst code. For more information, refer to Code Burst compiles.
The test can't find Understudy or the code under test
Symptom: the Console shows CS0246: The type or namespace name 'Understudy' could not be
found, or the same error for one of your own types.
Cause: the test assembly doesn't reference Understudy or the assembly of the
code under test, or the change to its references wasn't applied.
Resolution: select the test assembly definition, add both under Assembly Definition References in the Inspector, then select Apply. For more information, refer to Set up a test assembly.
The Test Runner lists no tests
Symptom: the Test Runner window shows no tests, or not the ones you wrote.
Cause: the test scripts aren't in a test assembly, the Console shows a compile error, or the tests are on the other tab: an assembly for every platform runs in Play mode, one for the Editor only in Edit mode.
Resolution: fix the compile errors first, then check the folder of your tests holds a test assembly definition, and look under both EditMode and PlayMode. For more information, refer to Create a test assembly.
Arg is ambiguous
Symptom: CS0104: 'Arg' is an ambiguous reference.
Cause: the file also uses another mocking library with an Arg class, such as
NSubstitute.
Resolution: add using Arg = Understudy.Arg; at the top of the file.
Nothing is woven
Symptom: the Console window shows an error that starts with Understudy: nothing is
woven, and every selection throws.
Cause: the Mono Cecil package is missing, or older than 1.10, so Understudy's compilation step didn't compile.
Resolution: install or update com.unity.nuget.mono-cecil, as the message says. For the
steps, refer to Install the package.
Weaving an assembly failed
Symptom: an error or a warning that starts with Understudy: weaving and names an
assembly that failed.
Cause: Understudy's compilation step couldn't rewrite that assembly. In a test assembly, its selections are unread, so every double it creates throws. In any other assembly, no spy, stub or fake reaches its code.
Resolution: report the full message through the contact form. Until it is fixed, if the assembly isn't a test assembly, add it to Excluded Assemblies, so the rest of the project is woven. For more information, refer to Exclude an assembly.
Understudy is for test code only
Symptom: an error that an assembly references Understudy, which is for test code only,
or that an assembly definition references Understudy without the UNITY_INCLUDE_TESTS define constraint.
Cause: an assembly that isn't a test assembly references Understudy. An assembly that references Understudy is never woven, so no double reaches its code. Without the define constraint, it also fails to compile in a player build.
Resolution: add UNITY_INCLUDE_TESTS to the assembly definition's
Define Constraints, or move the code that uses Understudy into a test assembly. For more information,
refer to Set up a test assembly.
The project settings cannot be read
Symptom: an error that the project settings in ProjectSettings/Understudy.json cannot
be read, so the defaults apply.
Cause: the file isn't valid JSON, for example after a merge conflict.
Resolution: fix the file, or delete it and set the settings again on the Understudy page of the Project Settings window.
A NonDeterministic declaration names no member
Symptom: an error that a [NonDeterministic] declaration names no member of its type.
Cause: the member name doesn't exist on the type, so leak detection ignores the declaration.
Resolution: fix the name in the declaration the error names. Use nameof, so that a
rename becomes a compile error.
Concordance is not supported in the same project
Symptom: an error, once per Editor session, that Concordance is not supported in the same project.
Cause: Understudy leaves every assembly that references Concordance unwoven, so no double reaches its code.
Resolution: remove one of the two packages.
A player build holds woven code
Symptom: a player build fails with an error that a file holds woven code, which a player build
must never contain.
Cause: a build without tests contains an assembly compiled for the Editor or for a build with tests, for example one a custom build step reuses.
Resolution: delete the Library/ScriptAssemblies folder and build again. For more
information, refer to Player builds.