public void SendStatementEmails(DateTime statementDate) { var housekeepers = _unitOfWork.Query <Housekeeper>(); foreach (var housekeeper in housekeepers) { if (String.IsNullOrWhiteSpace(housekeeper.Email)) { continue; } var statementFilename = _statementGenerator.SaveStatement(housekeeper.Oid, housekeeper.FullName, statementDate); if (string.IsNullOrWhiteSpace(statementFilename)) { continue; } var emailAddress = housekeeper.Email; var emailBody = housekeeper.StatementEmailBody; try { _emailSender.EmailFile(emailAddress, emailBody, statementFilename, string.Format("Sandpiper Statement {0:yyyy-MM} {1}", statementDate, housekeeper.FullName)); } catch (Exception e) { _messageBox.Show(e.Message, string.Format("Email failure: {0}", emailAddress), MessageBoxButtons.OK); } } }
// This is a Command Function that is also returning something like a Query Function... (Majority is Action -> Command) // So here comes Unit as well as Interaction Tests... // But no need of return statement here, since it is only present at the last. // so, we change return type to void and remove the return statement... (so no state based tests here) public void SendStatementEmails(DateTime statementDate) // We made this function non-static to ue Constructor Parameters Injection... { var housekeepers = _unitOfWork.Query <Housekeeper>(); // External Dependency 1 -> Unit Of Work // As all the housekeepers are got here, there's no sense of making another class for HouseKeeper... // So we make instance of UnitOfWork... foreach (var housekeeper in housekeepers) { if (String.IsNullOrWhiteSpace(housekeeper.Email)) { continue; } var statementFilename = _statementGenerator.SaveStatement(housekeeper.Oid, housekeeper.FullName, statementDate); // External Dependency 2 if (string.IsNullOrWhiteSpace(statementFilename)) { continue; } var emailAddress = housekeeper.Email; var emailBody = housekeeper.StatementEmailBody; try { _emailSender.EmailFile(emailAddress, emailBody, statementFilename, string.Format("Sandpiper Statement {0:yyyy-MM} {1}", statementDate, housekeeper.FullName));// External Dependency 3 } catch (Exception e) { _messageBox.Show(e.Message, string.Format("Email failure: {0}", emailAddress), MessageBoxButtons.OK);// External Dependency 4 - WPF (here we simply make an interface since class already present) } } }
// This function is more of Command type than Query type, hence we've to do Interaction testing here more than state based testing public void SendStatementEmails(DateTime statementDate) { // Returns List of Housekeeper objects from database var housekeepers = _unitOfWork.Query <Housekeeper>(); foreach (var housekeeper in housekeepers) { // Earlier it was housekeeper.Email == null which was breaking our test case. So we catched the bug and changed the implementation if (string.IsNullOrWhiteSpace(housekeeper.Email)) { continue; } var statementFilename = _statementGenerator.SaveStatement(housekeeper.Oid, housekeeper.FullName, statementDate); if (string.IsNullOrWhiteSpace(statementFilename)) { continue; } var emailAddress = housekeeper.Email; var emailBody = housekeeper.StatementEmailBody; try { _emailSender.EmailFile(emailAddress, emailBody, statementFilename, string.Format("Sandpiper Statement {0:yyyy-MM} {1}", statementDate, housekeeper.FullName)); } catch (Exception e) { _messageBox.Show(e.Message, string.Format("Email failure: {0}", emailAddress), MessageBoxButtons.OK); } } }
private static void TrySendStatementEmail(DateTime statementDate, Housekeeper housekeeper, string statementFilename) { try { _emailSender.EmailFile(housekeeper.Email, housekeeper.StatementEmailBody, statementFilename, CreateSubject(statementDate, housekeeper)); } catch (Exception e) { _xtraMessageBox.Show(e.Message, HousekeeperStatements(housekeeper), MessageBoxButtons.OK); } }
public bool SendStatementEmails(DateTime statementDate) { bool success = true; var housekeepers = _unitOfWork.Query <HouseKeeper>(); foreach (var housekeeper in housekeepers) { if (string.IsNullOrWhiteSpace(housekeeper.Email)) { continue; } var statementFilename = _statementGenerator.SaveStatement(housekeeper.Oid, housekeeper.FullName, statementDate); if (string.IsNullOrWhiteSpace(statementFilename)) { continue; } var emailAddress = housekeeper.Email; var emailBody = housekeeper.StatementEmailBody; try { _emailSender.EmailFile(emailAddress, emailBody, statementFilename, string.Format("Sandpiper Statement {0:yyyy-MM} {1}", statementDate, housekeeper.FullName)); } catch (Exception e) { success = false; break; } } return(success); }