Documentation

​
​

Development

User Acquisition

Monetization

Industry

Cloud Code

Scripting API

Services C# SDKs

Services JavaScript SDKs

Admin REST API

Client REST API

Scheduler Admin REST API

Observability REST API

Open Unity Dashboard

Cloud Code

LiveOps
​
​
Cloud Code
  • Overview
  • Get started
  • Server authority
  • Cloud Code C# modules
    • Overview
    • Get started
    • Concepts
    • Tutorials
      • Run modules
      • Write modules
      • Development essentials
        • Write unit tests
          • Create a unit test project
          • Create a unit test
        • Dependency injection
        • Custom serialization
        • Batch requests
        • In-memory cache
      • Automate deployment
      • Integrate services
      • Advanced configuration
      • Use Cases
    • Reference
  • Cloud Code JavaScript scripts
  • Logging
  • Use-case samples
  • Privacy and consent
  1. Cloud Code

Create a unit test

Add a unit test to your project using dependency injection patterns.
Read time 1 minute
Last updated 4 months ago

The following example tests the
TestInjection
Cloud Code method from the dependency injection guide. This example uses two separate mock instances of
IRandomNumber
. The first instance is injected through the class constructor, and the other instance is passed directly to the method. This example verifies that both injection points work correctly. The two instances use different values (
1
and
2
) so that you can identify which injection point failed if the assertion doesn't pass.
using ExampleModule;using NUnit.Framework;namespace TestExampleModule;public class MockedRandomNumber : IRandomNumber{ public int Number; public MockedRandomNumber(int number) { Number = number; } public int GetRandomNumber() { return Number; }}public class Tests{ private MockedRandomNumber mockedRandomNumberA; private MockedRandomNumber mockedRandomNumberB; [SetUp] public void Setup() { mockedRandomNumberA = new MockedRandomNumber(1); mockedRandomNumberB = new MockedRandomNumber(2); } [Test] public void TestDependencyInjection() { TestDependencyInjection dependencyInjection = new TestDependencyInjection(mockedRandomNumberA); DependencyInjectionResult result = dependencyInjection.TestInjection(mockedRandomNumberB); Assert.AreEqual(new DependencyInjectionResult { ConstructorNumber = mockedRandomNumberA.Number, MethodNumber = mockedRandomNumberB.Number, }, result, "Values are not the same"); }}
Run the unit test according to your IDE and confirm that the test passes.
Refer to Unit testing C# with NUnit and .NET Core (Microsoft) for more information.
To avoid publishing excess binaries when creating unit tests, refer to Create a unit test project.

Copyright © 2026 Unity Technologies
LegalPrivacy PolicyCookiesDocumentation Terms of UseDo Not Sell or Share My Personal InformationYour Privacy Choices (Cookie Settings)

"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S and elsewhere (more info here). Other names or brands are trademarks of their respective owners.

Some pages are machine-translated for convenience, and may contain inaccuracies. In the event of conflicting information, the English version is authoritative.


    Report a problem with this page