3

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.

Triad sou.
  • 2,969
  • 3
  • 23
  • 27
Jon Lawson
  • 582
  • 1
  • 6
  • 16
  • I am creating it with code: `var fake = A.Fake();` Is there something I can use on this: `A.CallTo(() => fake.ExecuteSqlCommand(A.Ignored, A.Ignored)). . .` ?? – Jon Lawson Sep 15 '11 at 14:22

1 Answers1

8

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);
Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
Patrik Hägne
  • 16,751
  • 5
  • 52
  • 60