Advertisement
corp0

Untitled

Sep 19th, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 KB | None | 0 0
  1. /******************************************************************************************
  2. * Example 1: How it is normally done.
  3. * This is bad because: hard to test, you depend on concrete classes and their details.
  4. *******************************************************************************************/
  5.  
  6. public class HttpThing
  7. {
  8.     public async string GetResponseAsString()
  9.     {
  10.         ... //details
  11.     }
  12. }
  13.  
  14. public class ClientCode
  15. {
  16.     private HttpThing http = new() HttpThing;
  17.    
  18.     public string OperateWithResponse()
  19.     {
  20.         var thing = http.GetResponseAsString;
  21.         // do something with thing
  22.         return result;
  23.     }
  24. }
  25.  
  26. [Test]
  27. public class TestClientCode
  28. {
  29.     [TestCase]
  30.     public void TestOperateWithResponse()
  31.     {
  32.         // Now I have to initialize the whole thing if I want to test this code
  33.         // and what if the response is different for whatever reason? What if I want to test and I don't have internet?
  34.     }
  35.  
  36. }
  37.  
  38. /******************************************************************************************
  39. * Example 2: How it could be.
  40. * This is good because: Easy to test, you don't depend on details, it both has dependency injection and dependency inversion.
  41. *******************************************************************************************/
  42.  
  43. public interface IHttpThing
  44. {
  45.     async string GetResponseAsString();
  46. }
  47.  
  48. public class HttpThing: IHttpThing
  49. {
  50.     public async string GetResponseAsString()
  51.     {
  52.         //details, same as first example
  53.     }
  54. }
  55.  
  56. public class ClientCode
  57. {
  58.     private IHttpThing http;
  59.    
  60.     public ClientCode(IHttpThing http)
  61.     {
  62.         this.http = http;
  63.     }
  64.    
  65.     public string OperateWithResponse()
  66.     {
  67.         var thing = http.GetResponseAsString;
  68.         // do something with thing
  69.         return result;
  70.     }
  71. }
  72.  
  73. public class MockHttpThing: IHttpThing
  74. {
  75.     public async string GetResponseAsString()
  76.     {
  77.         // I can set exactly what I expect to get
  78.         return "{'something': [1, 2, 3], 'another_thing': true}"
  79.     }
  80. }
  81.  
  82. [Test]
  83. public class TestClientCode
  84. {
  85.     public void TestOperationWithResponse()
  86.     {
  87.         var fakeHttp = new MockHttpThing();
  88.         var client = new ClientCode(fakeHttp);
  89.         // all my asserts and stuff that I can do in milisecond because I don't depend on anyone else.
  90.     }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement