나는 이걸 가지고 내 머리카락을 잡아 당길거야. 나는 보았고 Microsoft Moles에서 부분 스텁을 만들고 사용하는 간단하고 명확한 예를 찾을 수 없습니다. 어쩌면 내가 잃어버린 somethimg, 또는 내 코드를 잘 만들지 않을 수 있지만이 작동하도록 할 수 없습니다.Microsoft Moles에서 부분 스텁 만들기
public class AccountService : IAccountService {
private readonly webServiceProxy IExternalWebServiceProxy;
public AccountService(IExternalWebServiceProxy webServiceProxy) {
this.webServiceProxy = webServiceProxy;
}
public List<AccountModel> GetAccounts(string customerId) {
var returnList = new List<AccountModel>();
var xmlResponse = webServiceProxy.GetAllCustomerAccounts(customerId);
var accountNodes = xmlResponse.SelectNodes("//AccountNodes");
if (accountNodes != null)
{
foreach (XmlNode node in accountNodes)
{
var account = this.MapAccountFromXml(node);
if (!string.IsNullOrEmpty(account.AccountNumber))
{
returnList.Add(account);
}
}
}
return returnList;
}
public AccountModel MapAccountFromXml(XmlNode node) {
if (!IsValidAccount(node) {
return null;
}
// This performs a lot of XML manipulation getting nodes based on attributes
// and mapping them to the various properties of the AccountModel. It's messy
// and I didn't want it inline with the other code.
return populatedAccountModel;
{
public bool IsValidAccount(XmlNode node)
{
var taxSelectValue = node.SelectSingleNode("//FORMAT/Field[@taxSelect='1']").First().Value;
var accountStatus = // similar to first line in that it gets a single node using a specific XPath
var maturityDate = // similar to first line in that it gets a single node using a specific XPath
var maturityValue = // similar to first line in that it gets a single node using a specific XPath
return taxSelectValue != string.Empty && taxSelectValue != "0" && (accountStatusValue != "CL" || (maturityDate.Year >= DateTime.Now.AddYears(-1).Year));
}
}
는 내가하고 싶은 것은 내 GetAccounts() 메소드를 테스트 할 수 있습니다 :
여기 (간체) 내 수업입니다. IExternalWebServiceProxy 호출을 스텁 (stub)하여 가짜 XML을 반환 할 수 있지만 GetAccounts() 메서드가 MapAccountFromXml()을 호출하고 IsValidAccount()를 호출하기 때문에 내 서비스에서 내부 호출이 발생합니다.
아마도 해결책은 길고 관련이있는 MapAccountFromXml() 및 IsValidAccount() 코드를 분리하여 GetAccount() 호출에 인라인으로 넣는 것에 대해 걱정하지 않아도되지만 코드 가독성을 위해 코드를 분리하여 남겨 두는 것이 좋습니다.
내 두더지 어셈블리가 만든, 나는 내 문제는 내가 다음 MapAccountFromXml 및 IsValidAccount에 대한 내부 호출을 재정의하는 방법을 몰라이
var stubWebService = SIExternalWebServiceProxy {
GetAllCustomerAccounts = delegate {
return SomeHelper.GetFakeXmlDocument();
}
}
var stubAccountService = new SAccountService() { callsBase = true; }
처럼 내 클래스의 스텁 버전을 만들 수있어 나는 Unit Test가 thos 메소드를 테스트하는 것을 원하지 않는다. 테스트를 위해 GetAccounts를 분리하고 싶다. 메소드가 부분 스텁에서 오버라이드 될 가상 메소드가 필요하지만 어쨌든 테스트하고 싶은 메소드의 기반을 호출하는 동안 몇 가지 메소드를 오버라이드하는 스텁을 만드는 방법을 찾을 수 없습니다.
내 테스트 클래스를 만든 경우 테스트 한 메서드 당 하나의 클래스가 필요합니까? GetAccount를 테스트하려면 MapFromAccount 및 IsValidAccount가 스텁 된 클래스가 필요합니다. MapFromAccount를 테스트하기 위해서는 IsValidAccount가 스텁 된 다른 테스트 클래스와 IsValidAccount를 테스트하는 세 번째 클래스가 필요합니다. 차라리 특정 메서드에 대한 기반을 호출하는 스텁을 갖고 싶지만, Moles에서이를 수행하는 방법을 알아낼 수는 없습니다. 설명서에 명확한 예제가없는 것 같습니다. –
클래스를 스터핑하여 테스트 할 메소드 delegete에 기본 구현을 호출하고 스텁하려는 메소드에 대한 스텁을 만들 수도 있습니다. – Peter