Guest User

Untitled

a guest
May 20th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. var IRepository = new Mock<IRepository>();
  2. Request request = new Request();
  3. IRepository.Setup(a => a.Save(request)).Raises(a => a.Created += null, RequestCreatedEventArgs.Empty);
  4.  
  5. public class Request
  6. {
  7. //...
  8. }
  9.  
  10. public class RequestCreatedEventArgs : EventArgs
  11. {
  12. Request Request {get; set;}
  13. }
  14.  
  15. //=======================================
  16. //You must have sender as a first argument
  17. //=======================================
  18. public delegate void RequestCreatedEventHandler(object sender, RequestCreatedEventArgs e);
  19.  
  20. public interface IRepository
  21. {
  22. void Save(Request request);
  23. event RequestCreatedEventHandler Created;
  24. }
  25.  
  26. [TestMethod]
  27. public void Test()
  28. {
  29. var repository = new Mock<IRepository>();
  30. Request request = new Request();
  31. repository.Setup(a => a.Save(request)).Raises(a => a.Created += null, new RequestCreatedEventArgs());
  32.  
  33. bool eventRaised = false;
  34. repository.Object.Created += (sender, e) =>
  35. {
  36. eventRaised = true;
  37. };
  38. repository.Object.Save(request);
  39.  
  40. Assert.IsTrue(eventRaised);
  41. }
  42.  
  43. class IRepository
  44. {
  45. public event THING Created;
  46. }
  47. class THING : EventArgs
  48. {
  49. public static THING Empty
  50. {
  51. get { return new THING(); }
  52. }
  53. }
Add Comment
Please, Sign In to add comment