在 Expect.Call 中设置对象的属性
很难解释我正在寻找的内容,但我的例子应该可以澄清它。
我有下一个代码:
var schedule = ScheduleUtil.CreateScheduleDto(user, user);
Expect.Call(() => _scheduleRepository.Save(schedule));
现在,我想做的是,当进行此 Save 调用时,应将 Schedule.id 属性设置为另一个值(例如 1)。
我不想嘲笑时间表。这可以做到吗? Save 方法不返回值,因此不可能,但我确实希望修改对象计划。
更新:也许一个小例子可以澄清我到底想要什么。
假设有一个带有 Save 方法的类:
public void Create(EntityEntity) { //实体保存到数据库 //entity.id 使用数据库中创建的 id 进行更新 所以
,在创建之前,entity.id是-1,创建之后是> 0.
现在,有一个服务使用此 Create。该服务方法的代码契约规定,在调用之前,实体的 id 必须等于 -1,在调用之后,实体的 id 必须大于 -1。 0(前提条件和后置条件)。
所以,我需要的是这样的: var 实体 = 新实体(); //id == -1 Expect.Call(() => _instance.Create(实体);
//现在entity.id应该是一个随机数> 0.这就是我需要的,让Rhino Mocks将实体的id更新为给定的整数。这可能吗?
It's kind of hard to explain what I'm searching for but my example should clarify it.
I have next code:
var schedule = ScheduleUtil.CreateScheduleDto(user, user);
Expect.Call(() => _scheduleRepository.Save(schedule));
Now, what I want to do is when this Save call is made, the schedule.id property should be set to another value (1 for instance).
I do not want to mock schedule. Can this be done? The Save method doesn't return a value, so that's not a possiblity, but I do want the object schedule to be modified.
UPDATE: Maybe a small example will clarify what I exactly want.
Say there's a class with a method Save:
public void Create(Entity entity)
{
//entity is saved to database
//entity.id is updated with the created id in database
}
So, before the create, entity.id is -1, after the create it is > 0.
Now, there's a service that uses this Create. Code contracts on this service method say that before it is called, the entity must have an id equal to -1, after it is called it must have an id > 0 (preconditions and postconditions).
So, what I need is something like this:
var entity = new Entity(); //id == -1
Expect.Call(() => _instance.Create(entity);
//Now the entity.id should be a random number > 0. This is what I need, to have Rhino Mocks update the id of entity to a given integer. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不会。如果您没有模拟
_scheduleRepository
,Rhino Mocks 就不会知道它。为什么不想模拟_scheduleRepository
?编辑:好的,现在我明白你想做什么了。使用“WhenCalled”扩展方法来定义当Rhino.Mocks拦截调用时要执行的代码。像这样的东西应该有效:
No. If you're not mocking the
_scheduleRepository
, Rhino Mocks doesn't know about it. Why don't you want to mock the_scheduleRepository
?EDIT: Ok, now I see what you want to do. Use the "WhenCalled" extension method to define code to be executed when Rhino.Mocks intercepts the call. Something like this should work: