How to update a property on a parameter using FakeItEasy

旧城冷巷雨未停 提交于 2019-12-02 04:36:15

问题


I have an interface that includes a member that looks like:

void ExecuteSqlCommand(string procedureName, SqlParameter[] parameters);

I am using FakeItEasy to create a mock of this to pass to one of my classes.

The code I am testing calls this method, then checks the value of one of the SqlParameters. How do I use FakeItEasy to set the Value property of this parameter when the method is called?

I appreciate that this is probably not the best practice for getting individual pieces of information out of a database, but I am working with existing stored procedures, some of which have OUT parameters.


回答1:


As you say, this is probably not the best practice. That aside, I guess you could do something like this:

A.CallTo(() => fake.ExecuteSqlCommand(A<string>._, A<SqlParameter[]>._))
    .Invokes((string s, SqlParameter[] p) => p[someIndex].Value = yourValue);

Or, using a less-readable but more powerful overload, access a IFakeObjectCall directly:

A.CallTo(() => fake.ExecuteSqlCommand(A<string>._, A<SqlParameter[]>._))
    .Invokes(callObject => callObject.GetArgument<SqlParameter[]>("parameters")[someIndex].Value = yourValue);


来源:https://stackoverflow.com/questions/7432380/how-to-update-a-property-on-a-parameter-using-fakeiteasy

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