Return to Snippet

Revision: 66795
at September 24, 2014 01:51 by heathbo


Updated Code
Example 1:

private MockRepository repository = new MockRepository(MockBehavior.Loose);
private Mock<IEmailService> mockEmailService = repository.Create<IEmailService>();
private EmailUtility target = new EmailUtility(mockEmailService.Object);


// Setup
const string emailAddress = @"[email protected]";
const string messageSubject = @"foo example";
const string messageBody = @"foo body";
const bool highPriority = true;
const string fromAccount = "TestAccount";

var attachmentList = new List<Attachment>();

attachmentList.Add(new Attachment(new StreamClass(), ""));
attachmentList.Add(new Attachment(new StreamClass(), ""));
attachmentList.Add(new Attachment(new StreamClass(), ""));

// Action
target.SendEmail(emailAddress, messageSubject, messageBody, true, fromAccount, attachmentList);

// Assert
mockEmailService.Verify(x => x.SendMail(It.Is<MailMessage>(p => p.Attachments.Count() == 3), It.IsAny<bool>()));

------------------------------

Example 2: 

private MockRepository mockRepository;
private Mock<IProvider> mockProvider;

  var exception = new BiProcessorException("Test Exception");
  var brokerInvoiceModel = new BrokerInvoiceModel(){BrokerInvoiceId = 123,
      BrokerInvoiceNumber = "BI456"};

  mockProvider.Setup(h => h.BiProcessingErrorProvider.InsertBiProcessingError(
      It.Is<BiProcessingError>(x => x.BrokerInvoiceId == brokerInvoiceModel.BrokerInvoiceId
      && x.BrokerInvoiceNumber == brokerInvoiceModel.BrokerInvoiceNumber
      && x.ExceptionInfo == exception.Message
      && x.ExtendedInfo == exception.ToString()))).Verifiable();

// Action
  target.CreateErrorLog(exception, brokerInvoiceModel);

// Assert
  mockRepository.VerifyAll();

Revision: 66794
at July 3, 2014 23:51 by heathbo


Initial Code
private MockRepository repository = new MockRepository(MockBehavior.Loose);
private Mock<IEmailService> mockEmailService = repository.Create<IEmailService>();
private EmailUtility target = new EmailUtility(mockEmailService.Object);


// Setup
const string emailAddress = @"[email protected]";
const string messageSubject = @"foo example";
const string messageBody = @"foo body";
const bool highPriority = true;
const string fromAccount = "TestAccount";

var attachmentList = new List<Attachment>();

attachmentList.Add(new Attachment(new StreamClass(), ""));
attachmentList.Add(new Attachment(new StreamClass(), ""));
attachmentList.Add(new Attachment(new StreamClass(), ""));

// Action
target.SendEmail(emailAddress, messageSubject, messageBody, true, fromAccount, attachmentList);

// Assert
mockEmailService.Verify(x => x.SendMail(It.Is<MailMessage>(p => p.Attachments.Count() == 3), It.IsAny<bool>()));

Initial URL

                                

Initial Description
Example 1: I'm verifying that the list that is passed in has 3 attachments.  SendMail is run later in the SendEmail method.

Example 2: I'm verifying that the object that is passed into the method has the appropriate values.

Initial Title
Moq: check to see the value of passed in parameters.

Initial Tags
c#

Initial Language
C#