/// <summary> /// Gets all the settings for the mock session /// </summary> /// <returns></returns> public static MockSettings RetrieveSettings() { StackTrace stackTrace = new StackTrace(); StackFrame[] stackFrames = stackTrace.GetFrames(); var testMethodFrames = ( from StackFrame frame in stackFrames where (frame.GetMethod().GetCustomAttribute <TestMethodAttribute>() != null) || (frame.GetMethod().GetCustomAttribute <TestInitializeAttribute>() != null) || (frame.GetMethod().GetCustomAttribute <TestCleanupAttribute>() != null) || (frame.GetMethod().GetCustomAttribute <ClassInitializeAttribute>() != null) || (frame.GetMethod().GetCustomAttribute <ClassCleanupAttribute>() != null) select frame).ToArray(); StackFrame testMethodFrame = testMethodFrames.FirstOrDefault(); MockSettings settings = new MockSettings(); if (testMethodFrame != null) { settings.mockId = GetMockId(testMethodFrame); RecordMockDataResultsAttribute recordAttr = FindRecordMockDataResultsAttribute(testMethodFrame); if (recordAttr != null) { settings.recordingMode = true; settings.outputPath = recordAttr.OutputPath; settings.isolatedQueries = recordAttr.IsolatedQueries; } } else { // Leave the rest of settings as defaults (nulls and false) } return(settings); }
private static RecordMockDataResultsAttribute FindRecordMockDataResultsAttribute(StackFrame testMethodFrame) { MethodBase testMethod = testMethodFrame.GetMethod(); // Try to find RecordMockDataResultsAttribute. // 1) On the method: RecordMockDataResultsAttribute recordAttr = testMethod.GetCustomAttribute <RecordMockDataResultsAttribute>(); // 2) On nearest of the enclosing types. if (recordAttr == null) { for (Type currentType = testMethod.DeclaringType; currentType != null; currentType = currentType.DeclaringType) { recordAttr = currentType.GetCustomAttribute <RecordMockDataResultsAttribute>(); if (recordAttr != null) { break; } } } // 3) On the test assembly if (recordAttr == null) { recordAttr = testMethod.DeclaringType.Assembly.GetCustomAttribute <RecordMockDataResultsAttribute>(); } // 4) On the executing assembly if (recordAttr == null) { recordAttr = Assembly.GetExecutingAssembly().GetCustomAttribute <RecordMockDataResultsAttribute>(); } return(recordAttr); }