가상 메서드로 클래스를 조인하면 메서드를 확인할 수 있습니다. CallBase
을 사용하여 실제 메소드 (Update
)를 동시에 호출 할 수도 있습니다. 다음 코드를 가정
은 (나는 분열에서 불필요한 심판을 삭제했습니다 게시 된 업데이트는 ref
매개 변수를 포함하지 않는 주어진) :
public class Type
{
public string currency { get; set; }
public int OrderNo { get; set; }
}
public class BaseClass
{
// w.r.t ref, do you mean to reassign objList to the filtered lists?
public void SplitList(ref IList<Type> objList)
{
var SplitA = objList.Where(c => c.currency == "USD").ToList();
var SplitB = objList.Where(c => c.currency == "GBR").ToList();
if (SplitA.Count() > 0)
{
Update(SplitA);
}
if (SplitB.Count() > 0)
{
Update(SplitB);
}
}
public virtual IList<Type> Update(IList<Type> updateList)
{
int count = 0;
foreach (Type objType in updateList)
{
objType.OrderNo = count++;
}
return updateList;
}
}
이 단위 테스트는 가상 Update
방법이 될 수 있음을 보여줍니다 검증 된 것은 물론 호출된다. 이 CallBase
이 아닌을 선택한 경우 목록의 요소에 대한 변이가 발생하지 않으므로 단위 테스트를 조정해야합니다.
[Test]
public void TestSplitList()
{
var mock = new Mock<BaseClass>();
mock.CallBase = true; // This will ensure the actual Update also gets called
IList<Type> fakeTypes = new List<Type>
{
new Type {currency = "GBR"},
new Type {currency = "GBR", OrderNo = 100},
new Type {currency = "JPY", OrderNo = 55}
};
mock.Object.SplitList(ref fakeTypes);
mock.Verify(m => m.Update(It.IsAny<IList<Type>>()), Times.Exactly(1));
mock.Verify(m => m.Update(It.Is<IList<Type>>(x => x.Any(y => y.currency == "GBR")
&& x.Count == 2)), Times.Exactly(1));
mock.Verify(m => m.Update(It.Is<IList<Type>>(
x => x.Any(y => y.currency == "JPY"))), Times.Never);
Assert.AreEqual(3, fakeTypes.Count, "List itself must not have changed");
// These tests show the effect of the actual `Update` method
Assert.IsTrue(fakeTypes.Any(t => t.OrderNo == 0 && t.currency == "GBR"),
"GBR must be ordered");
Assert.IsTrue(fakeTypes.Any(t => t.OrderNo == 1 && t.currency == "GBR"),
"GBR must be ordered");
Assert.IsFalse(fakeTypes.Any(t => t.OrderNo == 100 && t.currency == "GBR"),
"GBR must be ordered");
Assert.IsTrue(fakeTypes.Any(t => t.OrderNo == 55 && t.currency == "JPY"),
"JPY must not be ordered");
}
참고 : List
매개 변수에 대한 ref
의 사용은 현재 코드 구현에 필요하지 않습니다.목록의 개체를 직접 변형하기 때문에이 변경 내용은이 항목을 참조하는 모든 컬렉션에서 볼 수 있습니다. 또한 목록 자체 (요소 만 포함)를 변경하지 않으므로 Update
매개 변수 ref
을 만들거나 Update
에서 반환하지 않아도됩니다.
이 우리에게 업데이트 메소드 구현 –
public 가상 업데이트보기 (IList의 updateList) { \t INT 카운트 = 0; \t foreach (updateList에 형식 objType) \t { \t \t objType.OrderNo = count ++; \t} \t return updateList; } 이와 비슷한 것입니다. –
Anshul