gpt4 book ai didi

c# - 我如何在最小起订量中设置这个给我 null 或一个对象?

转载 作者:行者123 更新时间:2023-11-30 21:34:14 25 4
gpt4 key购买 nike

我想看到我不关心 Find 子句中的内容。给我 null。

这里是测试

[Fact]
public void VerifyIndexViewType()
{
// Arrange
var mockUserProvider = new Mock<IUserProvider>();
mockUserProvider.Setup(x => x.GetUserId()).Returns("any-value-here");
var mockUnitOfWork = new Mock<IUnitOfWork>();
//how would I just return an object or null for example.. this doesnt work
// mockUnitOfWork.Setup(x => x.UserProfileDataRepository.Find(u => u.ApplicationUserId == "any value here")).Returns((IRepository<UserProfileData>)null);

var controller = new ProfileController(mockUnitOfWork.Object, mockUserProvider.Object);

// Act
var result = controller.Update();

// Assert
Assert.IsType<ViewResult>(result);
}

对于下面的 Controller 和 Action

public class ProfileController : BaseController
{
private IUnitOfWork _unitOfWork;
private readonly IUserProvider _requestUserProvider;

public ProfileController(IUnitOfWork unitOfWork,
IUserProvider requestUserProvider)
: base(unitOfWork, requestUserProvider)
{
_unitOfWork = unitOfWork;
_requestUserProvider = requestUserProvider;
}

public IActionResult Update()
{
//this is easy
string userId = _requestUserProvider.GetUserId();
//how do I do the setup in moq for this?
IEnumerable<UserProfileData> userProfileQuestions = _unitOfWork.UserProfileDataRepository.Find(x => x.ApplicationUserId == userId);

if (userProfileQuestions != null)
{
ProfileViewModel profileViewModel = new ProfileViewModel();
profileViewModel.UserProfileData = userProfileQuestions.FirstOrDefault();
return View(profileViewModel);
}

return View("Error", "Home");
}

编辑 1:我的 IUnitOfWork 和方法的实现

  public interface IUnitOfWork
{
IRepository<ApplicationUser> ApplicationUserRepository { get; }
IRepository<RefMedicalSpecialty> RefMedicalSpecialtyRepository { get; }
IRepository<RefProgramDetailData> RefProgramDetailDataRepository { get; }
IRepository<RefProgramProfileData> ProgramProfileDataRepository { get; }
IRepository<UserProgram> UserProgramRepository { get; }
IRepository<UserFeedback> UserFeedbackRepository { get; }
IRepository<UserAction> UserActionRepository { get; }
IRepository<UserProfileData> UserProfileDataRepository { get; }
IRepository<RawCredential> RawCredentialRepository { get; }
IRepository<RefProgramCharacteristic> RefProgramCharacteristicRepository { get; }
IRepository<UserProgramRefProgramCharacteristic> UserProgramRefProgramCharacteristicRepository { get; }

void Commit();
void RejectChanges();
void Dispose();
}

public IRepository<UserProfileData> UserProfileDataRepository =>
new Repository<UserProfileData>(_retContext);

最佳答案

将方法 Find 声明为虚拟的

 public virtual YourType Find(Expression<Func<YourClass, bool>> yourfunc)

模拟为:

mockUnitOfWork.Setup(x => x.UserProfileDataRepository.Find(It.IsAny<Expression<Func<YourClass, bool>>>()).Returns(DefineYourReturn);

关于c# - 我如何在最小起订量中设置这个给我 null 或一个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50496014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com