[Test]
public void NullSafeSetWithMinDateTimeTest()
{
var test = new DateTime(2008, 01, 31);
var index = 0;
var paramsMock = new Mock<IDataParameterCollection>();
var valueMock = new Mock<IDataParameter>();
valueMock.Setup(o => o.Value);
paramsMock.Object.Add(valueMock.Object);
_mock.Setup(o => o.Parameters).Returns(paramsMock.Object);
var obj = new DateTimeUserType();
obj.NullSafeSet(_mock.Object, test, index);
Assert.That(((IDataParameter)paramsMock.Object
[index]).Value, Is.EqualTo("20080131"));
}
But, it throws an exception when the NullSafeSet method attempts to
set the value of the first index.
So, in short, how do I mock the Parameters property of the IDbCommand
interface?
This is using the old "Expect/Verify" syntax, but maybe it will help.
Basically you need to set up a "Get" on the command so that your mocked
IDataParameterCollection is returned when the code asks for cmd.Parameters.
Once that's done, you can mock out the "CreateParameter" method on the
collection and verify that the mocked param's value was set as you expected.
Matt
var cmd = factory.Create<IDbCommand>();
var parms = factory.Create<IDataParameterCollection>();
var idParam = factory.Create<IDbDataParameter>();
cmd.ExpectSet(c => c.CommandText,
EntriesDbRepository.GetPigSexCommandText);
cmd.ExpectSet(c => c.CommandType, CommandType.Text);
cmd.ExpectSet(c => c.Transaction, txn);
cmd.Expect(c => c.CreateParameter()).Returns(idParam.Object);
cmd.ExpectGet(c => c.Parameters).Returns(parms.Object);
cmd.Expect(c => c.ExecuteScalar()).Returns(sex);
cmd.Expect(c => c.Dispose());
idParam.ExpectSet(p => p.ParameterName, "id");
idParam.ExpectSet(p => p.Value, id);
parms.Expect(p => p.Add(idParam.Object)).Returns(0);
return cmd;
--------------------------------------------------
From: "caloggins" <gsa...@gmail.com>
Sent: Friday, January 01, 2010 3:13 AM
To: "Moq Discussions" <moq...@googlegroups.com>
Subject: [Moq] IDbCommand.Parameters
> --
> Post: moq...@googlegroups.com
> Unsubscribe: moqdisc-u...@googlegroups.com
>
>
On Jan 3, 5:08 pm, Daniel Cazzulino <k...@clariusconsulting.net>
wrote: