我正在尝试构建一个单元测试。 类位置在第三方库中实现。但是对于我的单元测试,我需要将Size属性设置为特定值。
public class Position
{
private double _size;
private double Size
{
get
{
return _size;
}
internal set
{
_size = value;
}
}
}
我读过这篇文章:How do you create a unit-testing stub for an interface containing a read-only member? 但无法弄清楚如何使它适合我。
这是被测试的课程(只是一个简化的例子)。 pos
方法中的CalcPositionMetric()
参数必须为Position
类型:
public class PositionMetrics
{
public PositionMetrics()
{}
public double CalcPositionMetric(Position pos)
{
return 2 * pos.Size;
}
}
以下是我的单元测试:
using NUnit.Framework;
using NMock;
[TestFixture]
public class PositionUnitTests
{
[Test]
public void TestPosition()
{
Mock<Position> tmpPosMock = mFactory.CreateMock<Position>();
tmpPosMock.Expects.One.GetProperty(v => v.Size).WillReturn(7); /* !!! Exception !!! System.ArgumentException : mock object position has a getter for property Size, but it is not virtual or abstract */
/* Execute Test with tmpPositions*/
PositionMetrics pm = new PositionMetrics();
double result = pm.CalcPositionMetric(tmpPosMock.MockObject)
Assert.AreEqual(14, result);
}
}
但是你可以看到我得到一个例外。有人可以帮我解决这个问题吗?还欢迎任何其他解决方案!
干杯 康斯坦丁
答案 0 :(得分:1)
更新问题的新答案我建议您为此介绍某种代理接口。请参阅以下代码:
interface IPosition {
int Size { get; }
}
class Position { //in 3rd party lib
public int Size {
get { return 5; }
}
}
class RealPosition : IPosition { //use this as your real object instead of using Position directly
private Position position;
public RealPosition(Position position) {
this.position = position;
}
public int Size {
get { return position.Size; }
}
}
class MockPosition : IPosition { //use this for testing
public int Size{ get; set; }
}
public class Program {
static void Main(string[] args) {
var pos = new MockPosition { Size = 7 };
Console.WriteLine(Calc(pos)); //prints 14
Console.ReadLine();
}
static int Calc(IPosition pos) { //change your method signature to work with interface
return pos.Size * 2;
}
}
旧答案如果课程不是sealed
,则您不需要任何模拟库。只需使用new
修饰符即可获得所需的属性:
class Position {
public int Size { get { return 5; } }
}
class MockPosition : Position {
public new int Size { get; set; }
}
....
var mock= new MockPosition();
mock.Size = 7;
要在某种列表中使用这些项目,您必须像这样投射它们:
var items = new List<Position>();
for (int i = 0; i < 5; i++) {
items.Add(new MockPosition { Size = i });
}
foreach (var item in items.Cast<MockPosition>()) {
Console.Write("{0}\t", item.Size); //prints 0 1 2 3 4
}
如果它是密封的并且属性不是虚拟的,那么我将不得不使用其他一些技术,Moq
(我猜你正在使用)不允许这样做