使用C#.NetFrameWork 4.7,Moq 1.4
我正在测试一个进行2次接口调用的方法。接口方法采用1个参数,该参数也是一个接口。 Moq很难测试,抛出了铸造错误。
连续2次通话,签名如下:
InterfaceUnderTest.MethodUnderTest(IEnumerable<IParamInterface>)
完整的代码示例可在github上找到:https://github.com/JayZhang727/UnitTestingInterfaceParams
以下是基本结构:
我无法更改的导入的接口和实现:
public interface IWorkInterface
{
string DoWork(IEnumerable<IParamInterface> para);
}
public interface IParamInterface
{
IParamDataInterface GetParamData();
}
public interface IParamDataInterface
{
string Id { get; set; }
}
public class ParamClass<T> : IParamInterface where T : IParamDataInterface
{
public T ParamData { get; set; }
public ParamClass()
{
}
public ParamClass(T para)
{
this.ParamData = para;
}
public IParamDataInterface GetParamData()
{
return ParamData;
}
}
public class ParamClassA : IParamDataInterface
{
public ParamClassA()
{
this.Id = "Id";
this.ParamClassAVar = 123;
}
public string Id { get; set; }
public int ParamClassAVar { get; set; }
}
public class ParamClassB : IParamDataInterface
{
public ParamClassB()
{
this.Id = "Id";
this.ParamClassBVar = "not 123";
}
public string Id { get; set; }
public string ParamClassBVar { get; set; }
}
这是要测试的课程:
public ClassUnderTest(IWorkInterface workInt)
{
this.WorkClient = workInt;
}
public IWorkInterface WorkClient { get; set; }
public string MethodUnderTest()
{
var result = string.Empty;
//Class A is an implementation of IParamDataInterface
var a = new ParamClassA();
var theParamA = new ParamClass<ParamClassA>(a);
var listA = new List<IParamInterface>();
listA.Add(theParamA);
result = WorkClient.DoWork(listA);
//Class B is also an implementation of IParamDataInterface
var b = new ParamClassB();
var theParamB = new ParamClass<ParamClassB>(b);
var listB = new List<IParamInterface>();
listB.Add(theParamB);
result = result + WorkClient.DoWork(listB);
return result;
}
这是我尝试过的测试电话:
private Mock<IWorkInterface> mockClient;
[TestMethod()]
public void DoWorkTest()
{
mockClient = new Mock<IWorkInterface>(MockBehavior.Strict);
var target = new ClassUnderTest(mockClient.Object);
var mockSquence = new MockSequence();
mockClient.InSequence(mockSquence).Setup(ec => ec.DoWork(It.Is<List<IParamInterface>>(el => ((ParamClassA)((ParamClass<ParamClassA>)el[0]).GetParamData()).ParamClassAVar == 123))).Returns("123");
mockClient.InSequence(mockSquence).Setup(ec => ec.DoWork(It.Is<List<IParamInterface>>(el => ((ParamClassB)((ParamClass<ParamClassB>)el[0]).GetParamData()).ParamClassBVar == "not 123"))).Returns("not 123");
//act
target.MethodUnderTest();
//assert
mockClient.VerifyAll();
}
我收到有关无法从A类转换为B类的错误:
Message: Test method ClassUnderTesting.UnitTests.ClassUnderTestingTests.DoWorkTest threw exception:
System.InvalidCastException: Unable to cast object of type 'ClassUnderTesting.ParamClass`1[ClassUnderTesting.ParamClassA]' to type 'ClassUnderTesting.ParamClass`1[ClassUnderTesting.ParamClassB]'.
Moq似乎没有按顺序执行,由于某种原因,第二个设置胜过第一个设置,而顺序似乎无济于事。有人知道我应该如何设置和测试这两个电话吗?
答案 0 :(得分:1)
我会无序执行此操作。只需设置您的期望,以便在用ParamClassA
调用DoWork时返回“ 123”,而当用ParamClassB
调用DoWork时返回“不是123”。
请注意,如果没有给定ParamClassA
,第一个期望也不会崩溃:它根本不会匹配(同样,第二个期望和ParamClassB
也是如此)。
mockClient
.Setup(ec => ec.DoWork(
It.Is<List<IParamInterface>>(el => el[0] is ParamClass<ParamClassA> && ((ParamClass<ParamClassA>)el[0]).ParamData.ParamClassAVar == 123)
))
.Returns("123");
mockClient
.Setup(ec => ec.DoWork(
It.Is<List<IParamInterface>>(el => el[0] is ParamClass<ParamClassB> && ((ParamClass<ParamClassB>)el[0]).ParamData.ParamClassBVar == "not 123")
))
.Returns("not 123");