title: Unit test project - NUnit reference, throw exception
body: <p>I have unit test project and I wrote one test to check one functionality:</p>
  <pre><code>[Test]
      public async Task should_not_be_able_register_user_when_user_with_given_name_already_exists()
      {
          var mockUserRepository = new Mock&lt;IUserRepository&gt;();
  
          var userService = new UserService(mockUserRepository.Object);
  
          mockUserRepository.Setup(x =&gt; x.AddAsync(user));
          await userService.RegisterAsync(&quot;user&quot;, &quot;userLastName&quot;, &quot;fakeuser&quot;, &quot;user@example.com&quot;, &quot;123456789&quot;);
      }
  </code></pre>
  <p>In this test I want to check when user exist throw my defined exception but I have problems.</p>
  <ol>
  <li><p>When I invoke await userService.RegisterAsync... how write assert ?</p>
  <p>Assert.Throws(() =&gt; what should be here ?</p>
  </li>
  </ol>
  <p>My code:
  RegisterAsync looks like:</p>
  <pre><code>public async Task RegisterAsync(string firstName, string lastName, string username, string email, string phoneNumber)
      {
          var user = await _userRepository.GetAsync(username);
  
          if (user != null)
          {
              throw new CoreException(ErrorCode.UsernameExist, $&quot;Username {user.Username} already exist.&quot;);
          }
  
          user = new User(firstName, lastName, username, email, phoneNumber);
          await _userRepository.AddAsync(user);
      }
  </code></pre>
  <p>GetAsync:</p>
  <pre><code>public async Task&lt;User&gt; GetAsync(string username)
          =&gt; await _context.Users.SingleOrDefaultAsync(x =&gt; x.Username == username);
  </code></pre>