gpt4 book ai didi

c# - 对象未设置为犀牛模拟中对象的实例

转载 作者:行者123 更新时间:2023-11-30 14:56:51 24 4
gpt4 key购买 nike

更新:3

我正在尝试模拟一个类用于通过工作单元创建其他类的新实例的方法。当我尝试模拟返回固定数据的方法时,在调用 getPage 方法时我得到一个 null 而不是列表。

这是我的代码

[TestFixture()]
public class CustomerServiceTests
{
private ICustomerService service;
private IUnitOfWork mockUnitOfWork;
private IGenericRepository<Entities.Customer> repository;

private int customerId;
private int ContactId;

[SetUp()]
public void Setup()
{
customerId = 1;
ContactId = 1;
}

[Test()]
public void GetCustomers_should_return_three_results()
{
mockUnitOfWork = MockRepository.GenerateMock<IUnitOfWork>();
repository = MockRepository.GenerateMock<IGenericRepository<Entities.Customer>>();

List<Entities.Customer> customerList = new List<Entities.Customer>
{
new Entities.Customer { Id = 1, CompanyName = "test1", ContractorId = 1 },
new Entities.Customer { Id = 2, CompanyName = "test2", ContractorId = 2 },
new Entities.Customer { Id = 3, CompanyName = "test3", ContractorId = 1 },
new Entities.Customer { Id = 4, CompanyName = "test4", ContractorId = 1 },
new Entities.Customer { Id = 5, CompanyName = "test5", ContractorId = 4 }
};


var IQueryableList = customerList.AsEnumerable();
mockUnitOfWork.Stub(uow => uow.CustomerRepository).Return(repository);




repository.Stub(repo => repo.GetPaged()).Return(new ContentList<Entities.Customer> { List = IQueryableList, Total = customerList.Count });

service = new CustomerService(mockUnitOfWork);

var resultList = service.GetCustomers(new PageRequest {PageSize = 20, PageIndex = 1 });
var total = resultList.Data.Total;
Assert.AreEqual(10, total);
}

服务代码部分返回 null 而不是提供的列表。

            customers = _service.CustomerRepository.GetPaged(filter, orderBy, pageRequest.PageSize, pageRequest.PageIndex, "CustomersContacts");

最佳答案

您为不带参数的 GetPaged 设置了 stub

GetPaged()

但是你在调用带参数的 GetPaged

GetPaged(filter, orderBy, pageRequest.PageSize, pageRequest.PageIndex, "CustomersContacts")

尝试这样的事情(你需要验证语法,确保它是正确的类型)

repository
.Stub(repo => repo.GetPaged(
Arg<string>.Is.Anything,
Arg<string>.Is.Anything,
Arg<int>.Is.Anything,
Arg<int>.Is.Anything,
Arg<string>.Is.Anything))
.Return(new ContentList<Entities.Customer> { List = IQueryableList, Total = customerList.Count });

关于c# - 对象未设置为犀牛模拟中对象的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21706986/

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