Created
April 9, 2014 01:30
-
-
Save jefferydutra/10217214 to your computer and use it in GitHub Desktop.
I am hoping to use State Verification without having to resort to tedious TransformToDto methods.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IMappingService | |
{ | |
TDest Map<TSrc, TDest>(TSrc source) where TDest : class; | |
ICollection<TDest> Map<TSrc, TDest>(ICollection<TSrc> source) where TDest : class; | |
} | |
public class MappingService : IMappingService | |
{ | |
public TDest Map<TSrc, TDest>(TSrc source) where TDest : class | |
{ | |
return Mapper.Map<TSrc, TDest>(source); | |
} | |
public ICollection<TDest> Map<TSrc, TDest>(ICollection<TSrc> source) where TDest : class | |
{ | |
return Mapper.Map<ICollection<TSrc>, ICollection<TDest>>(source); | |
} | |
} | |
/// <summary> | |
/// Tests | |
/// </summary> | |
public class GetByIdTests{ | |
[Theory, AutoMoqData] | |
public void Returns_Result_Of_MappingService( | |
[Frozen]Mock<IMappingService> mappingService, | |
DecisionGraphService sut, | |
DecisionGraphDto expected, | |
GetDecisionGraphRequest request) | |
{ | |
mappingService | |
.Setup(r => r.Map<DecisionGraph, DecisionGraphDto>(It.IsAny<DecisionGraph>())) | |
.Returns(expected); | |
// Act | |
var actual = sut.GetDecisionGraph(request); | |
// Assert | |
Assert.Equal(expected,actual); | |
} | |
[Theory, AutoMoqData] | |
public void MappingService_Is_Called_With_Result_Of_Repository( | |
[Frozen(As = typeof(IDecisionGraphRepository))]DecisionGraphRepositoryStub repository, | |
[Frozen]Mock<IMappingService> mappingService, | |
DecisionGraphService sut, | |
GetDecisionGraphRequest request) | |
{ | |
var expected = GetExpectedEntity(repository); | |
request.Id = expected.Id; | |
// Act | |
sut.GetDecisionGraph(request); | |
// Assert | |
mappingService | |
.Verify(r => r.Map<DecisionGraph, DecisionGraphDto> | |
(It.Is<DecisionGraph>(actual => actual == expected))); | |
} | |
} | |
/// <summary> | |
/// System Under Test | |
/// </summary> | |
public class DecisionGraphService { | |
private readonly IMappingService _mappingService; | |
private readonly IDecisionGraphRepository _decisionGraphRepository; | |
public DecisionGraphService( | |
IMappingService mappingService, | |
IDecisionGraphRepository decisionGraphRepository) | |
{ | |
_mappingService = mappingService; | |
_decisionGraphRepository = decisionGraphRepository; | |
} | |
public DecisionGraphDto GetDecisionGraph(GetDecisionGraphRequest request) | |
{ | |
var decisionGraph = _decisionGraphRepository.FindBy(request.Id); | |
return decisionGraph == null | |
? null | |
: _mappingService.Map<DecisionGraph, DecisionGraphDto>(decisionGraph); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment