Uploaded April 2020 | Updated November 2026, 3 hours ago
Mocking describes a feature in programming that allows you to replace the functionality of an object with a blank version of itself or provide your own functionality in its place. This allows you to isolate code paths throughout your program to more easily target and test distinct units within your code. The advantage of this isolation is that your test code becomes smaller and more manageable and the scope of individual tests is reduced allowing you to reduce the flakiness of your test cases (a test is "flaky" if the test fails when the unit of code it is testing is functioning correctly - or has non-deterministic behavior). In order to accomplish all of this in .NET we're going to be exploring [Moq](github.com/moq/moq4).
Moq is a framework for .NET that makes it easy to generate and test mocked code. Given a class you are able to replace that class with a clone that matches all of the type requirements of the class but has none of the implementation leaving the rest up to you. It is compatible with most testing frameworks, but because we're hoping to later integrate with Unity 3D as part of our [Platforming game](youtube.com/playlist?list=PLEwYhelKHmigkz97T76r2bCt7wfG_1pPE) we'll be testing our integration of Moq with [NUnit](nunit.org/).
This framework is new to me so lets explore it together and cover some of the basic use cases that I'd like to employ Moq for: Mocking the value of object Properties, Mocking the value of object Functions and Mocking the results of a specific function invocation. From that hopefully we'll be able to build a better understanding of the capabilities of Moq and how we can best use it to improve the quality of our testing.
***
To add Moq to your dotnet project you can run:
```ps1
dotnet add package Moq
```
Moq is also on GitHub. You can browse the source and see code samples there: github.com/moq/moq4
Join the World of Zero Discord Server: discord.gg/hU5Kq2u
Mocking describes a feature in programming that allows you to replace the functionality of an object with a blank version of itself or provide your own functionality in its place. This allows you to isolate code paths throughout your program to more easily target and test distinct units within your code. The advantage of this isolation is that your test code becomes smaller and more manageable and the scope of individual tests is reduced allowing you to reduce the flakiness of your test cases (a test is "flaky" if the test fails when the unit of code it is testing is functioning correctly - or has non-deterministic behavior). In order to accomplish all of this in .NET we're going to be exploring [Moq](github.com/moq/moq4).
Moq is a framework for .NET that makes it easy to generate and test mocked code. Given a class you are able to replace that class with a clone that matches all of the type requirements of the class but has none of the implementation leaving the rest up to you. It is compatible with most testing frameworks, but because we're hoping to later integrate with Unity 3D as part of our [Platforming game](youtube.com/playlist?list=PLEwYhelKHmigkz97T76r2bCt7wfG_1pPE) we'll be testing our integration of Moq with [NUnit](nunit.org/).
This framework is new to me so lets explore it together and cover some of the basic use cases that I'd like to employ Moq for: Mocking the value of object Properties, Mocking the value of object Functions and Mocking the results of a specific function invocation. From that hopefully we'll be able to build a better understanding of the capabilities of Moq and how we can best use it to improve the quality of our testing.
***
To add Moq to your dotnet project you can run:
```ps1
dotnet add package Moq
```
Moq is also on GitHub. You can browse the source and see code samples there: github.com/moq/moq4
Join the World of Zero Discord Server: discord.gg/hU5Kq2u
 in an earlier video series. You can accomplish a lot with basic distance formula to create a number of very cool effects!
If you have suggestions for how we could improve this shader or ideas on what we should do with it next let me know in the comments!
Join the World of Zero Discord channel: https://discord.gg/hU5Kq2u A Static Glowing Ring - Ripple Shader - Part 1](https://i.ytimg.com/vi/EhkBdCdNx-Y/mqdefault.jpg)





 for recommending the use of `ContextMenu`s. You can find more of their content as a guest in videos at Infallible Code: https://www.youtube.com/user/charlesamat
### Code Examples
Some example uses of Unity 3Ds `ContextMenu` and `ContextMenuItem` Attributes:
#### `ContextMenu`
A `ContextMenu` Attribute is attached to functions in your `MonoBehavior`. When that menu item is clicked it will invoke the function the attribute is assigned to:
```csharp
[ContextMenu(Context Menu Example)]
void HelloContextMenu() {
Debug.Log(Context Menu Clicked!);
}
```
#### `ContextMenuItem`
A `ContextMenuItem` Attribute is attached to a field and requires you to provide the name of a function that will be invoked when the menu item is clicked.
```csharp
[ContextMenuItem(Context Menu Item Example, nameof(InvokeContextMenuItem))]
public float someVariable;
void InvokeContextMenuItem() {
Debug.Log(Context Menu Item Clicked!);
}
```
[`nameof`](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/nameof) is a C# function that returns a string representation for the name of the class, function, field, property etc that is referenced. Using this allows things like refactorings to function better and provides compiler errors if you spell a function name incorrectly (`nameof(FunctionName)` will throw an error if `FunctionName` is not defined).
Note: The functions invoked by both `ContextMenu` and `ContextMenuItem` should not have any arguments.
***
Unitys documentation on the `ContextMenu` Attribute: https://docs.unity3d.com/ScriptReference/ContextMenu.html
Unitys documentation on the `ContextMenuItem` Attribute: https://docs.unity3d.com/ScriptReference/ContextMenuItemAttribute.html
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Using ContextMenu and ContextMenuItem in Unity 3D](https://i.ytimg.com/vi/GKG-vUFt3D4/mqdefault.jpg)


![Building a Custom Dice Model with OpenSCAD
OpenSCAD is a tool that can compile programs into 3D models allowing you to code your own CAD designs instead of building them inside of a WYSIWYG type editor like Fusion 360, SketchUp etc.
The module we created for a basic dice looks like this:
```OpenSCAD
module CreateDice(size = 16) {
textSize = size * 0.75;
textLocation = size / 2 - 1;
textExtrusionSize = 2;
difference() {
cube([size, size, size], center = true);
translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(1, size=textSize, halign=center, valign=center);
rotate([180, 0, 0]) translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(6, size=textSize, halign=center, valign=center);
rotate([0, 90, 0]) translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(2, size=textSize, halign=center, valign=center);
rotate([0, 270, 0]) translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(5, size=textSize, halign=center, valign=center);
rotate([90, 0, 0]) translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(3, size=textSize, halign=center, valign=center);
rotate([270, 0, 0]) translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(4, size=textSize, halign=center, valign=center);
}
}
```
Get OpenSCAD: https://openscad.org/index.html
OpenSCADs documentation: https://openscad.org/documentation.html
OpenSCAD on GitHub: https://github.com/openscad/openscad/
You can get a copy of my models and the SCAD files that generate them here: https://github.com/runewake2/scad-examples
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Building a Custom Dice Model with OpenSCAD](https://i.ytimg.com/vi/HxiyUi4RQzw/mqdefault.jpg)
