Skip to content

Instantly share code, notes, and snippets.

@altunsercan
Last active August 2, 2020 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save altunsercan/95d076c4259f729240349478382bd645 to your computer and use it in GitHub Desktop.
Save altunsercan/95d076c4259f729240349478382bd645 to your computer and use it in GitHub Desktop.
Unity3d Wrapper Component Test Fixture

When using TDD with Unity3D, I often use Wrapper components to delegate calls to pure C# classes. This helps me test functionality in unit tests rather than running engine tests. Saves a lot of time and makes testing easier. However what if you make a mistake when making the wrapper component.

This testing fixture uses reflection to find interface property and methods and test them. All members are called using NSubstitute, if a call is not delegated to the wrapped object, test fails.

/*
MIT License
Copyright 2020 Sercan Altun (www.sercanaltun.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using NSubstitute;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
namespace TestingUtil
{
[TestFixture]
public abstract class ComponentWrapperTestFixture<TComponent, TWrapped>
where TComponent: MonoBehaviour, TWrapped, IWrapperComponent<TWrapped>
where TWrapped: class
{
public static IEnumerable<MethodInfo> WrappedMethodsSource()
{
var members = typeof(TWrapped).GetMembers();
foreach (MemberInfo memberInfo in members)
{
if (memberInfo.MemberType == MemberTypes.Method)
{
yield return (MethodInfo) memberInfo;
}
}
}
public static IEnumerable<PropertyInfo> WrappedPropertySource()
{
var members = typeof(TWrapped).GetMembers();
foreach (MemberInfo memberInfo in members)
{
if (memberInfo.MemberType == MemberTypes.Property)
{
yield return (PropertyInfo) memberInfo;
}
}
}
private WaitForEndOfFrame waitEndOfFrame = new WaitForEndOfFrame();
private object[] emptyParams = new object[0];
private TWrapped wrappedObject;
private TComponent wrapperComponent;
[UnitySetUp]
public IEnumerator Setup()
{
wrappedObject = Substitute.For<TWrapped>();
GameObject gameObject = new GameObject("wrapperObj", typeof(TComponent));
wrapperComponent = gameObject.GetComponent<TComponent>();
if (wrapperComponent != null)
{
wrapperComponent.SetWrappedObject(wrappedObject);
}
yield return waitEndOfFrame;
}
[UnityTest]
public IEnumerator TestMethodWrapping([ValueSource(nameof(WrappedMethodsSource))] MethodInfo methodInfo)
{
Assert.NotNull(methodInfo, "Invalid value source");
ParameterInfo[] parameterInfos = methodInfo.GetParameters();
object[] parameters = new object[parameterInfos.Length];
for (var index = 0; index < parameterInfos.Length; index++)
{
ParameterInfo parameterInfo = parameterInfos[index];
parameters[index] = MakeSubstituteType(parameterInfo.ParameterType);
}
methodInfo.Invoke(wrapperComponent, parameters);
methodInfo.Invoke(wrappedObject.ReceivedWithAnyArgs(1), parameters);
yield return waitEndOfFrame;
}
[UnityTest]
public IEnumerator TestPropertyWrapping([ValueSource(nameof(WrappedPropertySource))] PropertyInfo propertyInfo)
{
Assert.NotNull(propertyInfo, "Invalid value source");
MethodInfo getMethod = propertyInfo.GetMethod;
if (getMethod != null)
{
getMethod.Invoke(wrapperComponent, emptyParams);
getMethod.Invoke(wrappedObject.ReceivedWithAnyArgs(1), emptyParams);
}
MethodInfo setMethod = propertyInfo.SetMethod;
if (setMethod != null)
{
var substitute = MakeSubstituteType(propertyInfo.PropertyType);
setMethod.Invoke(wrapperComponent, emptyParams);
setMethod.Invoke(wrappedObject.ReceivedWithAnyArgs(1), emptyParams);
}
yield return waitEndOfFrame;
}
private object MakeSubstituteType(Type type)
{
if (type == null)
{
return null;
}
TypeInfo typeInfo = type.GetTypeInfo();
if (typeInfo == null)
{
return null;
}
if (typeInfo.IsValueType)
{
return Activator.CreateInstance(type);
}
else
{
return Substitute.For(new Type[]{type}, emptyParams);
}
}
}
}
/*
MIT License
Copyright 2020 Sercan Altun (www.sercanaltun.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
public interface IWrapperComponent<T>
{
void SetWrappedObject(T wrappedObject);
}
public interface Foo
{
event Action Evt;
int Field { get; set; }
void Method();
}
public class FooWrapperComponent : MonoBehavior, Foo, IWrapperComponent<Foo>
{
private Foo wrappedFoo;
void SetWrappedObject(Foo wrappedObject)
{
wrappedFoo = wrappedObject;
}
public event Action Evt
{
add => wrappedFoo.Evt += value;
remove => wrappedFoo.Evt -= value;
}
int Field { get => wrappedFoo.Field; set => wrappedFoo.Field = value; }
void Method() => wrappedFoo.Method();
}
public class FooWrapperComponentTests : ComponentWrapperTestFixture<FooWrapperComponent, Foo>{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment