Set a property of an object in a Expect.Call

你离开我真会死。 提交于 2019-12-13 13:51:05

问题


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?


回答1:


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:

_scheduleRepository.Expect(s => s.Save(schedule)).WhenCalled(a => ((Schedule) a.Arguments[0]).Id = 1);


来源:https://stackoverflow.com/questions/4387299/set-a-property-of-an-object-in-a-expect-call

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!