2014-11-24 7 views
3

필자는 많은 의존성을 가진 클래스를 테스트해야 할 때 FakeItEasy와 함께 AutoFixture를 사용하지만, 그 중 일부만 조롱하기로했다. 모든 종속성의 나머지 부분은 FakeItEasy의 Strict() 옵션을 조롱하는 것을 선호합니다. 내 테스트를 더 깨끗하게하기 위해서 필자는 원하는 의존성만을 조롱하고 모의하지 않는 모든 종속성을 Strict()로 생성하도록 지정할 수있다.FakeItEasy에서 AutoFixture를 사용하여 Strict로 명시 적으로 정의되지 않은 종속성을 등록하는 방법은 무엇입니까?

아래 예에서는 IDependency2 모의를 만드는 두 줄의 코드를 제거 할 수 있지만 동일한 동작을 유지할 수 있습니다. 테스트중인 클래스가 IDependency2의 ​​메서드에 액세스하는 경우 예외가 throw됩니다. .

어떻게하면 좋을까요? 트레이드 오프 솔루션으로

[TestFixture] 
public class AutoTests 
{ 
    [Test] 
    public void Test() 
    { 
     // Arrange 
     IFixture fixture = new Fixture().Customize(new AutoFakeItEasyCustomization()); 

     var dependency1 = fixture.Freeze<IDependency1>(); 
     A.CallTo(() => dependency1.DoSomething()).Returns(12); 

     // I do not want to specify these two lines 
     var dependency2 = A.Fake<IDependency2>(s=>s.Strict()); 
     fixture.Register(() => dependency2); 

     // Act 
     var classUnderTest = fixture.Create<ClassUnderTest>(); 
     var actualResult = classUnderTest.MethodUnderTest(); 

     // Assert 
     Assert.That(actualResult,Is.GreaterThan(0)); 
    } 
} 

public class ClassUnderTest 
{ 
    private readonly IDependency1 _dependency1; 
    private readonly IDependency2 _dependency2; 

    public ClassUnderTest(IDependency1 dependency1, IDependency2 dependency2) 
    { 
     _dependency1 = dependency1; 
     _dependency2 = dependency2; 
    } 

    public int MethodUnderTest() 
    { 
     return _dependency1.DoSomething() 
      + _dependency2.DoSomething(); 
    } 
} 

public interface IDependency1 
{ 
    int DoSomething(); 
} 

public interface IDependency2 
{ 
    int DoSomething(); 
} 
+3

왜 당신은 상관하지 않는 경우 테스트 복식은 엄격하고 싶어 그들에 대해서? –

+0

나는 그 의존성이 실수로 불려지지 않는다는 점에 유의한다. 명시 적으로 가짜가 아니며 사용 된 경우 - 테스트가 실패해야합니다. –

+1

실수로 전화하지 않는 것이 왜 중요합니까? 이러한 종속성에는 부작용이 있습니까? –

답변

3

은, 그것은 엄격한 가짜 모든 자동 생성 된 가짜를 사용자 정의 ISpecimenBuilder을 구현하는 것은 매우 간단합니다. 커튼 뒤에서 무슨 일이 벌어지고 있는지 알기 위해 표준 Ploeh.AutoFixture.AutoFakeItEasy.FakeItEasyRelay 가짜 빌더를 살펴볼 수 있습니다. 로 구현 엄격한 가짜에 대한 수정 된 사용자 정의 빌더는 다음과 같습니다

public class FakeItEasyStrictRelay : ISpecimenBuilder 
    { 
     public object Create(object request, ISpecimenContext context) 
     { 
      if (context == null) 
       throw new ArgumentNullException("context"); 

      if (!this.IsSatisfiedBy(request)) 
       return new NoSpecimen(request); 

      var type = request as Type; 
      if (type == null) 
       return new NoSpecimen(request); 

      var fakeFactoryMethod = this.GetType() 
       .GetMethod("CreateStrictFake", BindingFlags.Instance | BindingFlags.NonPublic) 
       .MakeGenericMethod((Type) request); 

      var fake = fakeFactoryMethod.Invoke(this, new object[0]); 

      return fake; 
     } 

     public bool IsSatisfiedBy(object request) 
     { 
      var t = request as Type; 
      return (t != null) && ((t.IsAbstract) || (t.IsInterface)); 
     } 

     private T CreateStrictFake<T>() 
     { 
      return A.Fake<T>(s => s.Strict()); 
     } 
    } 

이 할 수있는 간단하게 다음과 같은 성명에 등록 될 :

IFixture fixture = new Fixture().Customize(
       new AutoFakeItEasyCustomization(new FakeItEasyStrictRelay())); 
+0

대단히 감사합니다. 이것은 내가 찾고 있었던 바로 그 것이다. –