Guest User

Untitled

a guest
Aug 31st, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. Testing events with NUnit mock objects
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. using NUnit.Framework;
  8. using NUnit.Mocks;
  9.  
  10. namespace NUnitTest
  11. {
  12. public delegate void LoggedInDelegate();
  13.  
  14. public interface ISession
  15. {
  16. void LogIn(String username, String password);
  17. event LoggedInDelegate OnLoggedIn;
  18. }
  19.  
  20. public class App
  21. {
  22. private bool loggedIn = false;
  23. private ISession sess;
  24.  
  25. public bool LoggedIn
  26. {
  27. get
  28. {
  29. return loggedIn;
  30. }
  31. }
  32.  
  33. public App(ISession sess)
  34. {
  35. this.sess = sess;
  36. sess.OnLoggedIn += HandleOnLoggedIn;
  37. }
  38.  
  39. public void LogIn(String username, String password)
  40. {
  41. sess.LogIn(username, password);
  42. }
  43.  
  44. public void HandleOnLoggedIn()
  45. {
  46. loggedIn = true;
  47. }
  48. }
  49.  
  50. [TestFixture]
  51. public class AppTest
  52. {
  53. private String USERNAME = "Username";
  54. private String PASSWORD = "Password";
  55.  
  56. private DynamicMock mockSess;
  57. private App app;
  58.  
  59. [SetUp]
  60. public void TestInit()
  61. {
  62. // Create objects.
  63. mockSess = new DynamicMock(typeof(ISession));
  64. app = new App((ISession) mockSess.MockInstance);
  65. }
  66.  
  67. [Test]
  68. public void TestLogin()
  69. {
  70. mockSess.Expect("LogIn", USERNAME, PASSWORD);
  71. app.LogIn(USERNAME, PASSWORD);
  72. mockSess.Verify();
  73.  
  74. mockSess.Call("OnLoggedIn");
  75. Assert.IsTrue(app.LoggedIn);
  76. }
  77. }
  78. }
  79.  
  80. [TestFixture]
  81. public sealed class TestStubbingEvents
  82. {
  83. [Test]
  84. public void FooReceivesEventFromBar()
  85. {
  86. BarStub bar = new BarStub();
  87. Foo foo = new Foo(bar);
  88.  
  89. Assert.That(foo.EventReceived, Is.False);
  90. bar.RaiseBarEvent();
  91. Assert.That(foo.EventReceived, Is.True);
  92. }
  93.  
  94. }
  95.  
  96. internal class Foo
  97. {
  98.  
  99. public bool EventReceived
  100. {
  101. get; set;
  102. }
  103.  
  104. public Foo(IBar bar)
  105. {
  106. EventReceived = false;
  107. bar.BarEvent += ReceiveBarEvent;
  108. }
  109.  
  110. private void ReceiveBarEvent(object sender, EventArgs args)
  111. {
  112. EventReceived = true;
  113. }
  114.  
  115. }
  116.  
  117. internal class BarStub : IBar
  118. {
  119. public event BarEventHandler BarEvent;
  120.  
  121. //Stub method that invokes the event
  122. public void RaiseBarEvent()
  123. {
  124. BarEvent.Invoke(this, new EventArgs());
  125. }
  126. }
  127.  
  128.  
  129. public delegate void BarEventHandler(object sender, EventArgs args);
  130.  
  131. public interface IBar
  132. {
  133. event BarEventHandler BarEvent;
  134. }
Add Comment
Please, Sign In to add comment