title: FakeItEasy to test domain services + UnitOfWork
body: <p>I started doing some experimentation with unit testing so that we can include them in our domain layer. However i dont know if I'm following the right path, thus i'm going to explain what i'm currently doing to see if i'm on the right track. Basically the architecture is like the following there is Domain Layer containing domain models and domain services (ex. User class and UserService class). Then Domain layer communicates with the DAL which implements the Generic Repository pattern together with the Unit of Work. Each domain service class in it's constructor accepts an IUnitOfWork interface, like the following:</p>
  
  <pre><code>    public class UserService: IUserService
      {
          private readonly IUnitOfWork _unitOfWork;
  
          public UserService(IUnitOfWork unitOfwork)
          {
              this._unitOfWork = unitOfwork;
          }
  
      }
  </code></pre>
  
  <p>In order to creat the unit tests, i decided to go with FakeItEasy framework. So in a UserServiceTest class i did the following:-</p>
  
  <pre><code>  private IUserService _userService;
      private const int userID = 2013;
  
      [TestInitialize]
      public void Initialize()
      {
      _userService = A.Fake&lt;IUserService&gt;();
  
          A.CallTo(() =&gt; _userService.GetUserById(userID)).Returns(new User
              {
                  UserID = userID,
                  RegistrationDate = DateTime.Now,
              });
      }
  
  
      [TestMethod]
      public void GetUserByID()
      {
          var user = _userService.GetUserById(userID);
          Assert.IsInstanceOfType(user, typeof(Domain.User));
          Assert.AreEqual(userID, user.userID);
       }
  </code></pre>
  
  <p>When I run the tests, they pass. Is it the correct way of implementing unit tests? Before I was trying a different approach however FakeItEasy was failing with a ProxyGenerator exception. What i was doing is this:-</p>
  
  <pre><code> [TestInitialize]
   public void Initialize()
   {
  _unitOfWork = A.Fake&lt;IUnitOfWork&gt;();
  
  A.CallTo(() =&gt; _unitOfWork.UserRepository.FindById(userID)).Returns(new UserDto
      {
          UserID = userID,
          RegistrationDate = DateTime.Now,
      });
  
  
  AutoMapper.Mapper.CreateMap&lt;UserDto, User();
   }
  
   [TestMethod]
   public void GetUserByID()
   {
       var userService = new UserService(_unitOfWork);
       var user = userService.GetUserById(userID);
       Assert.IsInstanceOfType(user, typeof(Domain.User));
       Assert.AreEqual(userID, user.userID);
   }
  </code></pre>
  
  <p>And this was throwing the below exception:-</p>
  
  <pre><code>Result Message: 
  Initialization method Initialize threw exception. System.ArgumentNullException: System.ArgumentNullException: Value cannot be null.
  Parameter name: callTarget.
  Result StackTrace:  
  at FakeItEasy.Creation.ProxyGeneratorSelector.MethodCanBeInterceptedOnInstance(MethodInfo method, Object callTarget, String&amp; failReason)
     at FakeItEasy.Configuration.DefaultInterceptionAsserter.AssertThatMethodCanBeInterceptedOnInstance(MethodInfo method, Object callTarget)
     at FakeItEasy.Configuration.FakeConfigurationManager.AssertThatMemberCanBeIntercepted(LambdaExpression callSpecification)
     at FakeItEasy.Configuration.FakeConfigurationManager.CallTo[T](Expression`1 callSpecification)
     at FakeItEasy.A.CallTo[T](Expression`1 callSpecification)
  </code></pre>
  
  <p>Any feedback would be greatly appreciated. Thanks!</p>