How to update a property on a parameter using Fake

2020-04-16 19:02发布

问题:

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


标签: fakeiteasy