开发者

Set a property of an object in a Expect.Call

开发者 https://www.devze.com 2023-01-29 05:33 出处:网络
It\'s kind of hard to explain what I\'m searching for but my example should clarify it. I have next code:

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 mus开发者_运维知识库t 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?


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号