Embed constructor or not with TDD?

I have a method that I am trying to use unit test that uses a query object, I would like to overlay this query object on my unit tests. This request object has a dependency (UnitOfWork). I am using the IOC / DI container to instantiate objects in an application. However, I DO NOT want to use the container during TDD. As I can see, I have 2 options:

  • Add the request object to the method class as a field or property and enter it as the ctor argument. . However, this is not so, since this 1 method is the only method that will use it, and if I ever had to add a second method that used this request object, the object had to be re-created or Reset after each use.
  • Add the request object to the method signature. Smell?

Are there any other options or fixes for this? Or am I not mistaken?

Here are a few pseudo codes:

Option number 1

public class OrdersController
{
            public OrdersController(IOrderQuery query)
            {
                this.query = query;
            }

            private readonly IOrderQuery query;

            public Queryable<Order> OrdersWaiting()
            {
                var results = query(...);
                ...
            }
}

Option number 2

public class OrdersController
{
            public Queryable<Order> OrdersWaiting(IOrderQuery query)
            {
                var results = query(...);
                ...
            }
}

And my request object

public class OrderQuery : IOrderQuery
{
            public OrderQuery(IUnitOfWork unitOfWork)
            {
                ...
            }
}
+3
source share
2 answers

- , , Reset .

, , IOrderQueryFactory.

+1

1 2. , IOC , / . , / OrdersWaiting .

2 , , , , .

+1
source

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


All Articles