2012-07-30 3 views
1

몇 가지 단위 테스트를 작성하려면 Moles와 협력하고 있습니다. 온라인으로 검색했지만 Moles를 사용하여 AppSettingsReader.GetValue에 대한 호출을 차단하는 방법에 대한 응답은 표시되지 않습니다.Moles 및 AppSettingsReader?

누구든지 Moles를 사용하여이 작업을 수행 할 수 있습니까? 아니면 내가 주입하거나 조롱 할 수있는 자신의 수업에서 전화를 격리하도록 강요합니까? 이상적으로는 Moles를 직접 사용하여 호출을 가로채는 방법이 있습니다. 우리가 테스트 할 코드를 수정하고 싶지 않기 때문입니다.

감사합니다.

+0

무엇을 그 내가 전화를 포장 결국 가치 : 공용 클래스 appSettings는을 { 공용 개체 GetValue (문자열 키, 형식 유형) { return 새 AppSettingsReader(). GetValue (key, type); } } 테스트 할 코드를 수정하지 않기를 바란다면 여전히 해결책을 찾고 싶습니다. – EricR

답변

0

첫째, 난 강력하게/C 번호를 .NET 4.5, "페이크 및 스텁"이라고 두더지의 릴리스 버전, 5/비주얼 스튜디오 2012

System.Configurations 네임 스페이스를 이동하는 것이 좋습니다 것은 두더지와 호환되지 않습니다/위조 유형이며 스터브해야합니다. Moles Framework를 사용하여 스텁을 만들려면 System.Configuration.AppSettingsReader 유형의 인터페이스를 만듭니다. Moles 컴파일러는 인터페이스를 스텁 유형으로 자동 변환합니다. 도 그루터기 클래스 다음

using System; 

namespace YOUR_NAMESPACE_HERE 
{ 
    /// <summary> 
    /// IOC object for stubbing System.Configuration.AppSettingsReader. 
    /// Provides a method for reading values of a particular type from 
    /// the configuration. 
    /// </summary> 
    interface IAppSettingsReader 
    { 
    /// <summary> 
    /// Gets the value for a specified key from the 
    /// System.Configuration.ConfigurationSettings.AppSettings property 
    /// and returns an object of the specified type containing the 
    /// value from the configuration. 
    /// </summary> 
    /// <param name="key">The key for which to get the value.</param> 
    /// <param name="type">The type of the object to return.</param> 
    /// <returns>The value of the specified key</returns> 
    /// <exception cref="System.ArgumentNullException">key is null. 
    /// - or - 
    /// type is null.</exception> 
    /// <exception cref="System.InvalidOperationException">key does 
    /// not exist in the &lt;appSettings&gt; configuration section. 
    /// - or - 
    /// The value in the &lt;appSettings&gt; configuration section 
    /// for key is not of type type.</exception> 
    public object GetValue(string key, Type type); 
    } 
} 

것 : 여기

이 프로젝트에 추가 할 수있는 인터페이스입니다 들어

using System; 
using System.Configuration; 

namespace YOUR_NAMESPACE_HERE 
{ 
    /// <summary> 
    /// Production stub for System.Configuration.AppSettingsReader. 
    /// Provides a method for reading values of a particular type from 
    /// the configuration. 
    /// </summary> 
    public class AppSettingsReaderStub : IAppSettingsReader 
    { 
    /// <summary> 
    /// Gets the value for a specified key from the 
    /// System.Configuration.ConfigurationSettings.AppSettings property 
    /// and returns an object of the specified type containing the value 
    /// from the configuration. 
    /// </summary> 
    /// <param name="key">The key for which to get the value.</param> 
    /// <param name="type">The type of the object to return.</param> 
    /// <returns>The value of the specified key</returns> 
    /// <exception cref="System.ArgumentNullException">key is null. 
    /// - or - 
    /// type is null.</exception> 
    /// <exception cref="System.InvalidOperationException">key does not 
    /// exist in the &lt;appSettings&gt; configuration section. 
    /// - or - 
    /// The value in the &lt;appSettings&gt; configuration section for 
    /// key is not of type type.</exception> 
    public object GetValue(string key, Type type) 
    { 
     var reader = new AppSettingsReader(); 
     object result = reader.GetValue(key, type); 
     return result; 
    } 
    } 
} 
+0

VS 2012가 출시되지 않았기 때문에 아직 F & M을 사용할 수 없으며 올해는 사용하지 않을 것으로 예상됩니다. 스텁에 대한 정보를 보내 주셔서 감사합니다. 주입 모델을 사용하는 경우 유용 할 수 있습니다. 코드베이스를 감안할 때이 테스트 세트에 스텁을 사용할 수 없습니다. – EricR

관련 문제