gpt4 book ai didi

c# - 我如何最小化这个 IRepository?

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

我试图从基础开始,但无法理解如何创建此最小起订量对象。

 public class TestRefMedicalSpecialty
{
private Mock<IRepository<RefMedicalSpecialty>> mockRefMedicalRepo;

public TestRefMedicalSpecialty()
{
var mockRefMedicalSpecialties = new List<RefMedicalSpecialty>
{
new RefMedicalSpecialty {Code = "10000", Description = "Medical Specialty1"},
new RefMedicalSpecialty {Code = "10001", Description = "Medical Specialty2"},
new RefMedicalSpecialty {Code = "10002", Description = "Medical Specialty3"}
};

mockRefMedicalRepo = new Mock<IRepository<RefMedicalSpecialty>>();

mockRefMedicalRepo.Setup(repo => repo.GetAll()).Returns(mockRefMedicalSpecialties);

// this.mockRefMedicalRepo = new Mock<IRepository<RefMedicalSpecialty>>().Object;
this.mockRefMedicalRepo = mockRefMedicalRepo.Object;
}

[Fact]
public void GetAll_Returns_Data()
{

}
}

Severity Code Description Project File Line Suppression State Error CS0266 Cannot implicitly convert type 'Aamc.Ret.Data.IRepository<Aamc.Ret.Data.Models.RefMedicalSpecialty>' to 'Moq.Mock<Aamc.Ret.Data.IRepository<Aamc.Ret.Data.Models.RefMedicalSpecialty>>'. An explicit conversion exists (are you missing a cast?) Aamc.Ret.UnitTests

最佳答案

Mock<T>.Object属性返回通用参数的模拟 T .

所以在你的情况下mockRefMedicalRepo.ObjectIRepository<RefMedicalSpecialty>但您正试图将其分配给 Mock<IRepository<RefMedicalSpecialty>>如错误消息所述。

这里有一个可能的选择

public class TestRefMedicalSpecialty {
private IRepository<RefMedicalSpecialty> mockRefMedicalRepo;

public TestRefMedicalSpecialty() {
var mockRefMedicalSpecialties = new List<RefMedicalSpecialty>
{
new RefMedicalSpecialty {Code = "10000", Description = "Medical Specialty1"},
new RefMedicalSpecialty {Code = "10001", Description = "Medical Specialty2"},
new RefMedicalSpecialty {Code = "10002", Description = "Medical Specialty3"}
};

var mock = new Mock<IRepository<RefMedicalSpecialty>>();

mock.Setup(_ => _.GetAll()).Returns(mockRefMedicalSpecialties);

this.mockRefMedicalRepo = mock.Object;
}

[Fact]
public void GetAll_Returns_Data() {

//pass the mocked repo into a dependent class and
//exercise test that relies on calling GetAll

}
}

引用 Moq Quickstart更好地了解如何使用 Moq 框架

关于c# - 我如何最小化这个 IRepository?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50493966/

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