A unit testing class that has no output

Often, a class would not have a direct exit; he manipulates his dependencies, forcing them to do something.

For instance:

internal interface IEmailer
{
    void SendEmail();
}

class ReportSender
{
    public IEmailer Emailer { get; set; }

    public ReportSender(IEmailer emailerToUse)
    {
        Emailer = emailerToUse;
    }

    public void SendReport()
    {
        // Do whatever is needed to create the report

        Emailer.SendEmail();
    }
}

Creating an IEmailer layout and making it expect that IEmailer.SendEmail () seems to expose too many internal class functions and makes the test vulnerable. But I cannot think of another way to test this class.

How should we write unit tests for this class?

+3
source share
6 answers

Creating an IEmailer layout does not in itself display too much class. Rather, it makes it open to extensibilty .

, ( mocks), , , mocks.

, , void, , Moq Rhino Mocks, , , .
+3

Mock- - unit test . , . .

Rhino Mocks.

http://www.ayende.com/projects/rhino-mocks.aspx

IEmailer emailer = MockRepository.GenerateMock<IEmailer>();
emailer.Expect(x => x.SendEmail());

ReportSender reportSender = new ReportSender();
reportSender.Emailer = emailer;
reportSender.SendReport();

emailer.VerifyAllExpectations();
+2

IEmailer IEmailer.SendEmail()

- , .

UNIT, . , , .

+1

, ... , . , , IEmailer ?

0

IEmailer SendEmail - .

0

IEmailer , IEmailer.SendEmail(), , .

, ?

.

0
source

Source: https://habr.com/ru/post/1755535/


All Articles