In unit testing, there are few or no benefits when using dependency injection. Injection injection helps you create loose coupling components, and loose coupling components are easier to test, that is.
So, if you want to test some service, just create mock its dependencies and pass them to this service as usual (there is no need to include the IOC container here, I can hardly imagine that you will need some functions of IOC containers - such as context binding, interception and etc. - inside unit test).
If you want your ApplicationService to be easily tested, it should look more:
public class ApplicationService: IApplicationService { private readonly IEntityFrameworkRepo repo;
Here the dependency is passed by the constructor. In the application code, you will use it together with the IOC container to create an constructor injection (the IOC container will be responsible for instantiating IEntityFrameworkRepo ).
But in unit test, you can simply pass in an instance of some IEntityFrameworkRepo implementation created by yourself.
ApplicationDto
While ApplicationDto is some object that can be created manually, I can use it directly in unit-test (creating instances manually). Otherwise, I will have to wrap it with an interface like IApplicationDto in order to be able to make fun of it with Moq.
public class ApplicationDto{ public int Id {get; set;} public string Name {get; set;} }
Here is what the unit test would look like:
In the unit test, I will use the mocked implementation of IApplicationRepo , because I do not want to configure, for example. database connections, web services, etc., and my main intention is to test ApplicationService not the main repository. Another advantage is that the test will be run without a specific configuration for different machines. To create some kind of db repository I can use for example. List
[Test(Description = "Test If can successfully add application")] public void CanAddApplicationIfEligible() { var repo = GetRepo(); var appService = new ApplicationService(repo); var testAppDto = new ApplicationDto() { Id = 155, Name = "My Name" }; var currentItems = repo.ApplicationDtos.Count(); appService.Add(testAppDto); Assert.AreEqual(currentItems + 1, repo.ApplicationDtos.Count()); var justAdded = repo.ApplicationsDto.Where(x=> x.Id = 155).FirstOrDefault(); Assert.IsNotNull(justAdded);
Note:
The ninject.mockingkernel extension has been updated. The approach described in the wiki example can make your block test code bit tidier, but the approach described there is definitely not for injection (it's a service locator).