So I have some SMTP stuff in my code and I am trying to unit test that method.
So I been trying to Mockup MailMessage but it never seems to work. I think none of the met
You will end up mocking several different classes here (at least two). First, you need a wrapper around the MailMessage class. I would create an interface for the wrapper, then have the wrapper implement the interface. In your test, you will mock up the interface. Second, you'll provide a mock implementation as an expectation to the mocked interface for the MailAddressCollection. Since MailAddressCollection implements Collection, this should be fairly straight-forward. If mocking the MailAddressCollection is problematic due to additional properties (I didn't check), you could have your wrapper return it as an IList, which as an interface should be easy to mock.
public interface IMailMessageWrapper
{
MailAddressCollection To { get; }
}
public class MailMessageWrapper
{
private MailMessage Message { get; set; }
public MailMessageWrapper( MailMessage message )
{
this.Message = message;
}
public MailAddressCollection To
{
get { return this.Message.To; }
}
}
// RhinoMock syntax, sorry -- but I don't use Moq
public void MessageToTest()
{
var message = MockRepository.GenerateMock()
var to = MockRepository.GenerateMock();
var expectedAddress = "test@example.com";
message.Expect( m => m.To ).Return( to ).Repeat.Any();
to.Expect( t => t.Add( expectedAddress ) );
...
}