Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using Newtonsoft.Json;
- public static class MockUtils
- {
- /// <summary>
- /// Get a Deserialized Instance from a JSON file with Mock Data
- /// </summary>
- /// <example>
- /// Below demonstrates usage and assumes the existence of path/to/mocks/mock-file.mock(|-file).json
- /// <code>
- /// MyClass example1 = MockUtils.GetMockData<MyClass>("mock-file", "path/to/mocks/");
- /// MyClass example2 = MockUtils.GetMockData<MyClass>("mock-file", "path/to/mocks/", ".mock-file.json");
- /// </code>
- /// </example>
- /// <returns>Deserialized instance of JSON Mock.</returns>
- /// <param name="mockFile">Mock file.</param>
- /// <param name="mockPath">Mock path (Defaults to "mocks/").</param>
- /// <param name="fileExt">File ext (Defaults to ".mock.json").</param>
- /// <typeparam name="T">The 1st type parameter.</typeparam>
- public static T GetMockData<T>(string mockFile, string mockPath = "mocks/", string fileExt = ".mock.json")
- {
- mockFile = mockFile.Replace(".json", "").Replace(".mock", "");
- return JsonConvert.DeserializeObject<T>(
- System.IO.File.ReadAllText(@"" + mockPath + mockFile + fileExt)
- );
- }
- /// <summary>
- /// Get a Collection of Deserialized Instances from a JSON file with Mock Data
- /// </summary>
- /// <example>
- /// Below demonstrates usage and assumes the existence of path/to/mocks/mock-file.mock.json
- /// <code>
- /// MyClass example1 = MockUtils.GetMockDataCollection<MyClass>("mock-file", "path/to/mocks/");
- /// MyClass example2 = MockUtils.GetMockDataCollection<MyClass>("mock-file", "path/to/mocks/", ".mock-file.json");
- /// </code>
- /// </example>
- /// <returns>The mock data collection.</returns>
- /// <param name="mockFile">Mock file.</param>
- /// <param name="mockPath">Mock path (Defaults to "mocks/").</param>
- /// <param name="fileExt">File ext (Defaults to ".mock.json").</param>
- /// <typeparam name="T">The 1st type parameter.</typeparam>
- public static IEnumerable<T> GetMockDataCollection<T>(string mockFile, string mockPath = "mocks/", string fileExt = ".mock.json")
- {
- mockFile = mockFile.Replace(".json", "").Replace(".mock", "");
- return JsonConvert.DeserializeObject<IEnumerable<T>>(
- System.IO.File.ReadAllText(@"" + mockPath + mockFile + mockPath)
- );
- }
- }
Add Comment
Please, Sign In to add comment