Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. using HWSysManagerLib;
  2. bool ProcessBias(HWSysManager systemManager, string hwPath)
  3. {
  4. int handle = systemManager.OpenConfiguration(hwPath);
  5. ...
  6. // some magic goes here
  7. // return result
  8. }
  9.  
  10. public interface IHWSysManager
  11. {
  12. int OpenConfiguration(string hwPath);
  13. }
  14.  
  15. public class HWSysManagerImpl : IHWSysManager
  16. {
  17. private HWSysManager _hwSysManager; //Initialize from constructor
  18.  
  19. public int OpenConfiguration(string hwPath)
  20. {
  21. return _hwSysManager.openConfiguration(hwPath);
  22. }
  23. }
  24.  
  25. bool ProcessBias(IHWSysManager systemManager, string hwPath)
  26. {
  27. int handle = systemManager.OpenConfiguration(hwPath);
  28. ...
  29. // some magic goes here
  30. // return result
  31. }
  32.  
  33. var fakeManager = Isolate.Fake.Instance<HWSysManager>();
  34.  
  35. Isolate.WhenCalled(() => fakeManager.OpenConfiguration("")).WillReturn(0);
  36.  
  37. Isolate.WhenCalled(() => fakeManager.OpenConfiguration("")).DoInstead(
  38. context =>
  39. {
  40. //Your simulation
  41. });
  42.  
  43. class HWSysManager
  44. {
  45. public virtual int ExampleReturnIntMethod(int a)
  46. {
  47. var someInt = 0;
  48. return someInt;
  49. }
  50.  
  51. public void TestMethod()
  52. {
  53. Mock<HWSysManager> hwSysManager = new Mock<HWSysManager>();
  54. hwSysManager.Setup(x => x.ExampleReturnInMethod(It.IsAny<int> ())).Returns(10); //if parameter is any of int, return 10 in this case
  55. }
  56.  
  57. var hwSysInstance = hwSysManager.Object;
  58. var result = hwSysInstance.ExampleReturnInMethod(2); //result will be 10 in this case - as we have mocked
  59.  
  60. public interface HwsysManager
  61. {
  62. int OpenConfiguration(string hwPath);
  63. }
  64.  
  65. public void TestMethod()
  66. {
  67. Mock<HwsysManager> hwsysManager = new Mock<HwsysManager>();
  68.  
  69. hwsysManager.Setup(x => x.OpenConfiguration(It.IsAny<string>())).Returns(10);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement