public static void Main() { // This code shows that both service implementations have access to the database and // that they still fulfill the same interface contract IReportingService untestableReportingService = new Problem.ReportingService(); Console.WriteLine( "Students enrolled, queried via the untestable service implementation: {0}", untestableReportingService.GetNumberOfEnrolledStudents()); IReportingService testableReportingService = new Solution.ReportingService(); Console.WriteLine( "Students enrolled, queried via the testable service implementation: {0}", testableReportingService.GetNumberOfEnrolledStudents()); Console.ReadKey(); }
public void AlasHowDoYouTestThisWithoutAWorkingDatabaseConnection() { // Arrange IReportingService reportingService = new ReportingService(); // Act var actual = reportingService.GetNumberOfEnrolledStudents(); // The above will throw either a System.InvalidOperationException or a System.Data.SqlClient.SqlException, // or simply time out depending on wether you use the entity framework LocalDB feature. // // This exception and or time out is 100% valid, since unit tests should never expect a working database connection. // // See the 'Solution' namespaces for how to solve this problem by completing removing the database // connection from the picture while still retaining the ability to test the linq statements against // a specific set of data. // Assert - this line will never be reached Assert.AreEqual(5, actual); }