Guest User

Untitled

a guest
Nov 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Newtonsoft.Json;
  4.  
  5. public static class MockUtils
  6. {
  7. /// <summary>
  8. /// Get a Deserialized Instance from a JSON file with Mock Data
  9. /// </summary>
  10. /// <example>
  11. /// Below demonstrates usage and assumes the existence of path/to/mocks/mock-file.mock(|-file).json
  12. /// <code>
  13. /// MyClass example1 = MockUtils.GetMockData<MyClass>("mock-file", "path/to/mocks/");
  14. /// MyClass example2 = MockUtils.GetMockData<MyClass>("mock-file", "path/to/mocks/", ".mock-file.json");
  15. /// </code>
  16. /// </example>
  17. /// <returns>Deserialized instance of JSON Mock.</returns>
  18. /// <param name="mockFile">Mock file.</param>
  19. /// <param name="mockPath">Mock path (Defaults to "mocks/").</param>
  20. /// <param name="fileExt">File ext (Defaults to ".mock.json").</param>
  21. /// <typeparam name="T">The 1st type parameter.</typeparam>
  22. public static T GetMockData<T>(string mockFile, string mockPath = "mocks/", string fileExt = ".mock.json")
  23. {
  24. mockFile = mockFile.Replace(".json", "").Replace(".mock", "");
  25. return JsonConvert.DeserializeObject<T>(
  26. System.IO.File.ReadAllText(@"" + mockPath + mockFile + fileExt)
  27. );
  28. }
  29.  
  30. /// <summary>
  31. /// Get a Collection of Deserialized Instances from a JSON file with Mock Data
  32. /// </summary>
  33. /// <example>
  34. /// Below demonstrates usage and assumes the existence of path/to/mocks/mock-file.mock.json
  35. /// <code>
  36. /// MyClass example1 = MockUtils.GetMockDataCollection<MyClass>("mock-file", "path/to/mocks/");
  37. /// MyClass example2 = MockUtils.GetMockDataCollection<MyClass>("mock-file", "path/to/mocks/", ".mock-file.json");
  38. /// </code>
  39. /// </example>
  40. /// <returns>The mock data collection.</returns>
  41. /// <param name="mockFile">Mock file.</param>
  42. /// <param name="mockPath">Mock path (Defaults to "mocks/").</param>
  43. /// <param name="fileExt">File ext (Defaults to ".mock.json").</param>
  44. /// <typeparam name="T">The 1st type parameter.</typeparam>
  45. public static IEnumerable<T> GetMockDataCollection<T>(string mockFile, string mockPath = "mocks/", string fileExt = ".mock.json")
  46. {
  47. mockFile = mockFile.Replace(".json", "").Replace(".mock", "");
  48. return JsonConvert.DeserializeObject<IEnumerable<T>>(
  49. System.IO.File.ReadAllText(@"" + mockPath + mockFile + mockPath)
  50. );
  51. }
  52. }
Add Comment
Please, Sign In to add comment