小编szi*_*szu的帖子

Moq的Xunit和Mock数据

我是单元测试的新手,任何人都可以建议如何使用xUnit和Moq测试下面的公共方法(CreateUser),谢谢!

public async Task<bool> CreateUser(UserDTO newUser)
{
  newUser.CustomerId = _userResolverService.GetCustomerId();
  if (await CheckUserExists(newUser)) return false;
  var salt = GenerateSalt(10);
  var passwordHash = GenerateHash(newUser.Password, salt);

  await _usersRepository.AddAsync(new User()
  {
    Role = newUser.Role,
    CretedOn = DateTime.Now,
    CustomerId = newUser.CustomerId,
    Email = newUser.Email,
    FirstName = newUser.FirstName,
    LastName = newUser.LastName,
    PasswordHash = passwordHash,
    Salt = salt,
    UpdatedOn = DateTime.Now
  });

  return true;
}
Run Code Online (Sandbox Code Playgroud)

下面的私有方法检查用户是否存在只返回现有用户的数量

private async Task<bool> CheckUserExists(UserDTO user)
    {
      var users = await _usersRepository.GetAllAsync();
      var userCount = users.Count(u => u.Email == user.Email);
      return …
Run Code Online (Sandbox Code Playgroud)

.net c# unit-testing moq xunit

5
推荐指数
1
解决办法
2万
查看次数

在 C# 通用存储库 EF 中包括孩子的孩子

我面临一个小问题,希望你能帮忙,基本上我想在通用存储库模式中得到孩子的孩子,因为我有多重关系的关系表

我的 Repository 方法如下所示:

public IQueryable<TEntity> Find(Expression<Func<TEntity, bool>> predicate, params Expression<Func<TEntity, object>>[] includes)
    {
      var query = _entities.Where(predicate).AsQueryable();
      if (includes != null)
      {
        query = includes.Aggregate(query, (current, include) => current.Include(include));
      }
      return query;
    }

public async Task<IQueryable<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate, params Expression<Func<TEntity, object>>[] includes)
    {
      return await Task.Run(() => Find(predicate, includes));
    }
Run Code Online (Sandbox Code Playgroud)

我的型号:

产品:

public class Product : BaseEntity<long>
  {
    [MaxLength(100)]
    public string Name { get; set; }
    [MaxLength(100)]
    public string Barcode { get; set; }
    public int ShelfLife { …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework entity-framework-core .net-core

5
推荐指数
0
解决办法
1076
查看次数

我应该等待ValueTask <T>吗?

哪个是ValueTask的有效实现?缓存服务从缓存或DB返回数据.

public async ValueTask<IList<HrEmploymentDataCustom>> GetEmployeesFacts()
{
    try
    {
        var facts = (List<HrEmploymentDataCustom>) _memoryCache.Get("facts");
        return facts ?? await _accountService.GetEmploymentFacts(DetailsRequestType.All, null);
    }
    catch (Exception e)
    {
        var tc = new TelemetryClient();
        tc.TrackException(e);
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

这会是: var employeesFacts = await _cacheService.GetEmployeesFacts();

要么 var employeesFacts = _cacheService.GetEmployeesFacts().Result;

这里有点困惑.

c# asp.net-core c#-7.0

3
推荐指数
1
解决办法
899
查看次数