Modular domain objects

We have a requirement to add an event reminder when a user enters their email address on the event page. An event is another domain object. Our initial thought was to create a Customer domain object and its associated CustomerService:

public class CustomerService {
    public void AddEventReminder(string emailAddress, int eventId) {
       var customer = new Customer(emailAddress);
       customer.AddEmailReminder(eventId);
    }
}

How can we verify in unit test that the AddEmailReminder method actually called a new client?

My thoughts:

  • Use factory to create a client. It smells because I thought you should use a factory where there was some difficulty in creating the object.
  • Bad code. Maybe there is a better way to do this?
  • The magic of magic.

(, ), , ? , , . , .

+3
2

, , "mock Customer". mock Customer, AddEmailReminder. - :

public class CustomerService {
    public void AddEventReminder(string emailAddress, int eventId) {
       var customer = createCustomer(emailAddress);
       customer.AddEmailReminder(eventId);
    }

    protected Customer createCustomer(string emailAddress) {
       return new Customer(emailAddress);
    }
}

( #, ):

void testCustomerCreation() {
    /* final? */ Customer mockCustomer = new Customer("email");
    CustomerService customerService = new CustomerService() {
       protected Customer createCustomer(string emailAddress) {
           return mockCustomer;
       }            
    };

    customerService.AddEventReminder("email", 14);

    assertEquals(mockCustomer.EventReminder() /* ? */, 14);
}
+6

API- CustomerService

- , CustomerService? Anemic . ?

, - CustomerService, ...

, , :

public void AddEventReminder(Customer customer, int eventId)

, Int32 Object Object,

public void AddEventReminder(Customer customer, Event event)

, - ?

?

, . , , .

:

Event , , CustomerRepository, , Event. .

, EventRepository, , . .

, . , - .

+2

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


All Articles