public async Task AddDiscountToUserAsyncShouldThrowExceptionIfUserIsNull() { var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) .Options; var dbContext = new MyCalisthenicAppDbContext(options); IHttpContextAccessor httpContextAccessor = new HttpContextAccessor(); var mockMapper = new MapperConfiguration(cfg => { cfg.AddProfile(new MyCalisthenicAppProfile()); }); var mapper = mockMapper.CreateMapper(); var usersService = new UsersService(httpContextAccessor, dbContext, mapper); var coupon = new CouponViewModel { Coupon = CouponText, }; var exception = await Assert.ThrowsAsync <NullReferenceException>(async() => await usersService.AddDiscountToUserAsync(coupon)); Assert.IsType <NullReferenceException>(exception); }