Guest User

Untitled

a guest
Aug 14th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. How to mock a certain code using Rhino.Mocks?
  2. public interface IFlowFolderHandler
  3. {
  4. OperationResult Post(FlowFolder dto);
  5. }
  6.  
  7. public class FlowFolderHandler : IFlowFolderHandler
  8. {
  9. private readonly IResponse m_response;
  10. private readonly IEntityRepository m_entityRepository;
  11.  
  12. public FlowFolderHandler(IResponse response, IEntityRepository entityRepository)
  13. {
  14. m_response = response;
  15. m_entityRepository = entityRepository;
  16. }
  17.  
  18. public OperationResult Post(FlowFolder dto)
  19. {
  20. var ent = FlowFolderX.Cast(dto, true);
  21. m_entityRepository.Update(ent);
  22. var id = EntityIdComparer.IdToString(ent.Id);
  23. m_response.Headers["X-Entity-Id"] = id;
  24. return new OperationResult.Created
  25. {
  26. RedirectLocation = new Uri("FlowFolder/" + id, UriKind.Relative),
  27. ResponseResource = ent.GetEntityRelation(),
  28. };
  29. }
  30. }
  31.  
  32. private class EntityRepositoryMock : IEntityRepository
  33. {
  34. private readonly Action<IEntityBase> m_update;
  35. public EntityRepositoryMock(int id, EntityRelation entityRelation)
  36. {
  37. m_update = ent => EntityRepository.ApplySaveSideEffects(ent, id, entityRelation);
  38. }
  39. public IEntityBase Fetch(EntityId entityId) { throw new NotImplementedException(); }
  40. public void FetchByExpression(Type entityType, Expression expr, IList list, List<Pair<string, bool>> orderBy) { throw new NotImplementedException(); }
  41. public void Update(IEntityBase entity) { m_update(entity); }
  42. }
  43.  
  44. private class ResponseMock : IResponse
  45. {
  46. public ResponseMock() { Headers = new HttpHeaderDictionary(); }
  47. public IHttpEntity Entity { get { throw new NotImplementedException(); } }
  48. public HttpHeaderDictionary Headers { get; private set; }
  49. public void WriteHeaders() { throw new NotImplementedException(); }
  50. public bool HeadersSent { get { throw new NotImplementedException(); } }
  51. public int StatusCode
  52. {
  53. get { throw new NotImplementedException(); }
  54. set { throw new NotImplementedException(); }
  55. }
  56. }
  57.  
  58. [Test]
  59. [Factory("YieldPostFlowFolderData")]
  60. public void PostFlowFolder(int id, Uri uri, EntityRelation entityRelation, FlowFolder flowFolder)
  61. {
  62. var entityRepository = new EntityRepositoryMock(id, entityRelation);
  63. var response = new ResponseMock();
  64. var handler = new FlowFolderHandler(response, entityRepository);
  65.  
  66. var result = handler.Post(flowFolder);
  67.  
  68. Assert.AreEqual((int)HttpStatusCode.Created, result.StatusCode);
  69. Assert.AreEqual(id, int.Parse(response.Headers["X-Entity-Id"]));
  70. Assert.AreEqual(uri, result.RedirectLocation);
  71. SUC.Utils.AssertDeepEqual(entityRelation, result.ResponseResource);
  72. }
  73.  
  74. // Mocks
  75. var repositoryMock = MockRepository.GenerateMock<IEntityRepository>();
  76. var responseMock = MockRepository.GenerateMock<IResponse>();
  77.  
  78. repositoryMock.Expect(m => m.Update(null)).IgnoreArguments.WhenCalled(
  79. mi =>
  80. {
  81. IEntityBase passedInEntity = mi.Args[0] as IEntityBase;
  82. EntityRepository.ApplySaveSideEffects(passedInEntity, id, entityRelation);
  83. }
  84. ).Repeat.Any();
  85.  
  86. repositoryMock.Expect(m => m.Entity).Proeprtybehaviour();
  87.  
  88. repositoryMock.AssertWasCalled(m => m.Update(), mi => mi.Repeat.Once());
Add Comment
Please, Sign In to add comment