作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
更新: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/
我知道我能做到: IDateTimeFactory dtf = MockRepository.GenerateStub(); dtf.Now = new DateTime(); DoStuff(dtf
我有一段嵌入了 Rhino 的 Java 代码(省略了不相关的位): Context cx = Context.enter(); Scriptable scope = cx.initStandardO
我正在研究 Rhino (Mirth),我必须处理/解析具有以下结构的 XML: ... ... 我只想获取所有“foo”节点,尽可能避免使用循环。我一直在尝试类似的东西:
我是一名优秀的程序员,十分优秀!