Need help mocking sql command obj (input parameter to ExecuteNonQuery )..

738 views
Skip to first unread message

Lakshmi Priyanka

unread,
Jan 26, 2014, 10:31:02 AM1/26/14
to nsubs...@googlegroups.com
Hi ,

My code has interface IDatabaseManager . In one of the methods , I am making a db call .
//Code Implementation
public method X()
{
//constructing Sqlcommand object
command = new SQlcommand ();

//adding some input parameters to the command obj
.................

// adding 2 output parameters to the command obj

SqlParameter outparam1= new SqlParameter("@outparam1",SqlDbType.Int);

kstRequestIdParam.Direction = ParameterDirection.Output;

SqlParameter outparam2= new SqlParameter("@outparam2",SqlDbType.Int);

kstRequestIdParam.Direction = ParameterDirection.Output;

//makes DB call

DatabaseManager.ExecuteNonQuery(command);

//retrives ouput param value

int resvalue = outparam1.Value;

............


}

// In my test method,  - Test Code
I am using the code as below
//created DatabaseManager Stub (IDatabaseManager is the interface)
//registered it with unity container


In my test execution , I am need to get the required output parameter value that I set in the test method . I tried few ways but unable to make it . Please suggest any way if you have already encountered this.

Regards,
Lakshmi

David Tchepak

unread,
Jan 29, 2014, 10:10:35 PM1/29/14
to nsubs...@googlegroups.com
Hi Lakshmi,

Do you want to assert that DatabaseManager.ExecuteNonQuery() was called with a parameter that matches certain criteria?
Or do you need a reference to the parameter?

The former can be done with something like:
  DatabaseManager.Received().ExecuteNonQuery(Arg.Is<ICommand>(cmd => cmd.Parameters[0].Whatever))

The latter can be done with something like:

  SqlParameter myParameter = null;
  DatabaseManager.WhenForAnyArgs(x => x.ExecuteNonQuery(null)).Do(x => myParameter = x.Arg<SqlCommand>().Parameter[0]);
  //or
  DatabaseManager.ExecuteNonQuery(Arg.Do<SqlCommand>(cmd => myParameter = cmd.Parameter[0]);

For more info:

Hope this helps.

Regards,
David




--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nsubstitute...@googlegroups.com.
To post to this group, send email to nsubs...@googlegroups.com.
Visit this group at http://groups.google.com/group/nsubstitute.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages