Любые дополнительные вопросы приветствуются.
Класс расширения, включая пространство имен:
Код: Выделить всё
namespace DFA_Lib_Unit_Tests.Constraints
{
public class Is : NUnit.Framework.Is
{
public static VectorConstraint OnSameTile(object expected)
{
return new VectorConstraint(expected);
}
}
}
Код: Выделить всё
namespace DFA_Lib_Unit_Tests.Constraints
{
public static class CustomConstraintExtensions
{
public static VectorConstraint OnTile(this ConstraintExpression expression, object expected)
{
VectorConstraint constraint = new VectorConstraint(expected);
expression.Append(constraint);
return constraint;
}
}
}
Код: Выделить всё
using System.Numerics;
namespace DFA_Lib_Unit_Tests.Tests
{
public class TestClass
{
[Test]
public void TestMethod()
{
Vector2 firstVector = new Vector2(1, 1);
Vector2 secondVector = new Vector2(1, 1);
// When extension method is in namespace: 'Is' is an ambiguous reference between 'DFA_Lib_Unit_Tests.Counstraints.Is' and 'NUnit.Framework.Is'
// Otherwise, executes as expected. I do want to put it in a namespace however.
Assert.That(firstVector, Is.OnSameTile(secondVector));
// When extension method is not in a namespace, I still cannot use Is.Not and receive the following compile error:
// 'ConstraintExpression' does not contain a definition for 'OnSameTile' and no accessible extension method 'OnSameTile' accepting a first argument of type 'ConstraintExpression' could be found (are you missing a using directive or an assembly reference?).
Assert.That(firstVector, Is.Not.OnSameTile(secondVector));
}
}
}
Код: Выделить всё
namespace DFA_Lib_Unit_Tests.Constraints
{
public class VectorConstraint : Constraint
{
public object Expected { get; }
public VectorConstraint(object expected)
{
Expected = expected;
}
public override ConstraintResult ApplyTo(TActual actual)
{
// Comparison code here
}
}
}
Я также обнаружил, что класс расширения Is, а не если ему не требуется пространство имен, он должен находиться в том же пространстве имен, что и родительское пространство имен тестового класса, или в нем. Что мне кажется странным, поскольку такое ограничение не распространяется на другие классы расширений в моем проекте.
Подробнее здесь: https://stackoverflow.com/questions/788 ... -nunits-is
Мобильная версия