title: Unity and NUnit
body: <p>I am trying to perform unit test to written method but I am facing issue.</p>
  
  <p>Here is my UnityConfig class:</p>
  
  <pre><code>public class UnityConfig
  {
      #region Unity Container
      private static Lazy&lt;IUnityContainer&gt; container = new Lazy&lt;IUnityContainer&gt;(() =&gt;
      {
          var container = new UnityContainer();
          RegisterTypes(container);
          return container;
      });
  
      public static IUnityContainer GetConfiguredContainer()
      {
          return container.Value;
      }
      #endregion
  
      public static void RegisterTypes(IUnityContainer container)
      {
          container.RegisterInstance(AutoMapperConfig.Initialize());
          container.RegisterType&lt;IActiveDirectoryUserService, ActiveDirectoryUserService&gt;();
          container.RegisterType&lt;IUserRepository, InMemoryUserRepository&gt;();
          container.RegisterType&lt;IUserService, UserService&gt;();
      }
  }
  </code></pre>
  
  <p>Tested method:</p>
  
  <pre><code>public async Task RegisterAsync(string name, string email, string firstName, string lastName, string password)
  {
      var user = await _userRepository.GetBynameAsync(name);
  
      if (user != null)
      {
          throw new Exception($"User {name} already exists.");
      }
      else
      {
          var ActiveDirectoryUser = await _ActiveDirectoryUserService.GetBynameAsync(name);
  
          if (ActiveDirectoryUser == null)
          {
              throw new Exception($"User {name} does not exist in Active Directory");
          }
          else
          {
              var salt = new Guid().ToString("N");
              user = new User(name, ActiveDirectoryUser.Email, ActiveDirectoryUser.FirstName, ActiveDirectoryUser.LastName, password, salt);
              await _userRepository.AddAsync(user);
          }
      }
  }
  </code></pre>
  
  <p>Test method:</p>
  
  <pre><code>[Test]
  public async Task Test()
  {
      var userRepositoryMock = new Mock&lt;IUserRepository&gt;();
      var activeDirectoryUserMock = new Mock&lt;IactiveDirectoryUserService&gt;();
      var mapperMock = new Mock&lt;IMapper&gt;();
  
      var userService = new UserService(userRepositoryMock.Object, activeDirectoryUserMock.Object, mapperMock.Object);
  
      await userService.RegisterAsync("name", "user@emial.com", "first name", "last name", "password");
  
      userRepositoryMock.Verify(x =&gt; x.AddAsync(It.IsAny&lt;User&gt;()), Times.Once);
  }
  </code></pre>
  
  <p>Tested method works fine outside test but when I run test it fails. I ran test in debug mode and I found that it does not enter to </p>
  
  <pre><code>var user = await _userRepository.GetBynameAsync(name);
  
  var ActiveDirectoryUser = await _ActiveDirectoryUserService.GetBynameAsync(name);
  </code></pre>
  
  <p>Any idea why it does not enter to these methods during testing? Do I need add some entries to UnityConfig to make it work?</p>
  
  <p>-- EDIT</p>
  
  <pre><code>private static ISet&lt;User&gt; _users = new HashSet&lt;User&gt;
  {
      new User("user1", "ab@rmail.com", "firstnam1", "lastname1", "pass", "salt"),
      new User("user2", "ac@rmail.com", "firstnam2", "lastname2", "pass", "salt"),
      new User("user3", "ad@rmail.com", "firstnam3", "lastname3", "pass", "salt")
  };
  
  public async Task&lt;IEnumerable&lt;User&gt;&gt; GetAllAsync()
      =&gt; await Task.FromResult(_users);
  </code></pre>