【发布时间】:2020-06-12 15:54:13
【问题描述】:
我正在尝试使用 MOQ 设置具有通用功能的模拟接口。该函数具有以下符号:
public interface IWizard
{
bool Cast<TSpell>(TSpell spell)
where TSpell : SpellBase, IComponents;
}
当我尝试设置设置功能时,我似乎无法以直接的方式完成它。我不断收到消息,说不可能进行隐式转换。它包含以下文本:'没有从 'SpellBase' 到 'IComponents' 的隐式引用转换。
var wizard = new Mock<IWizard>();
wizard
.Setup(x => x.Cast(It.IsAny<SpellBase>())) // this line has an error
.Returns(true);
除了实现同时实现SpellBase 和IComponents 的基类之外,我还有哪些选择?这甚至可能吗?
编辑1: 我尝试通过以下方式实现 Piotr 的方法:
[Test]
public void Test_JsonConvert_Performace()
{
var wizard = new Mock<IWizard>();
wizard
.Setup(x => x.Cast(It.IsAny<TestSpell>()))
.Returns(true);
var result = wizard.Object.Cast(new RealSpell());
Assert.IsTrue(result);
}
public interface IWizard
{
bool Cast<TSpell>(TSpell spell)
where TSpell : SpellBase, IComponents;
}
public abstract class SpellBase
{
}
public interface IComponents
{
}
public class TestSpell : SpellBase, IComponents
{
}
public class RealSpell : SpellBase, IComponents
{
}
很遗憾我的测试失败了。
【问题讨论】:
标签: c# unit-testing generics moq