2014-04-08 1 views
2

사용할 서비스를 결정할 공장을 지정하고 있습니다. CommunicationJob 클래스의 유닛 테스트를 할 때 IIndex의 모의에 어려움을 겪고 있습니다.양키스 <TKey, TValue>

그리고이 문제는 _cs [n.GetType(). Name] null입니다. 누군가 내 문제에 대한 해결책이 있습니까? 한 가지 해결책은 테스트 전에 Autofac을 시작하는 것이지만, 테스트 컨텍스트에서 AutoFac을로드하는 방법을 알지 못합니다. 이 같은

내 시험보기 :

[Theory] 
[InlineData(0)] 
[InlineData(1)] 
[InlineData(2)] 
[InlineData(3)] 
public void WithNotifications(int numberOfNotifications) 
{ 
    var fixture = new TestCommunicationJobFixture(); 

    var sut = fixture.WithNotifications(numberOfNotifications).GetSut(); 
    sut.Do(new DateTime()); 

    fixture.MockCommunicationService.Verify(x => x["EmailNotification"].Send(It.Is<Notification>(z => z.Sent != null)), Times.Exactly(numberOfNotifications)); 
    fixture.MockNotificationRepo.Verify(x => x.Update(It.Is<Notification>(z => z.Sent != null), true), Times.Exactly(numberOfNotifications)); 
} 
+0

테스트 결과는 어떻게됩니까? –

+0

질문 업데이트 – Rikard

+0

GetSut()은 무엇을합니까? –

답변

4

그래서 나는 당신의 설정

public class Something 
{ 
    private readonly IIndex<string, IService> index; 
    public Something(IIndex<string, IService> index) 
    { 
     this.index = index; 
    } 

    public void DoStuff() 
    { 
     this.index["someString"].Send(); 
    } 
} 

public interface IIndex<TKey, TValue> 
{ 
    TValue this[TKey index] {get;set;} 
} 

public interface IService 
{ 
    void Send(); 
} 

에 비슷한 재현 한 그리고 그렇게 (MOQ를 사용하여) 같은 테스트 :

// Arrange 
var serviceMock = new Mock<IService>(); 

var indexMock = new Mock<IIndex<string, IService>>(); 
indexMock.Setup(x => x[It.IsAny<string>()]).Returns(serviceMock.Object); 

var something = new Something(indexMock.Object); 

// Act 
something.DoStuff(); 

// Assert 
serviceMock.Verify(x => x.Send()); 

바라기를 이것은 올바른 방향으로 당신을 가리킬 것입니다. 분명히 당신은 IRepo<Notification>을 모의 할 필요가 있습니다.

관련 문제