First create a mock object
var car = repository.StrictMock();
then set the expectation like follows:
using(repository.Record())
{
car.Name = "any";
LastCall.IgnoreArguments();
}
where mocked object has a settable parameter name. So your test expects a call to car.Name in the execution scope. Red line of code says you are expecting a set call. And the green line says ignore he value is being set. So this code just checking that the set_Name is getting invoked.
If you know what value is going to be set, then simply remove the green line.
This test does not need separate assertion about the value etc. Rhino Mocks framework takes care of that. Framework automatically all recorded expectation. If one missing or extra invocation happened in the execution scope, it will through exception and the test will fail.
Complete example is as follows:
-------------------------------------------------------------------------
using NUnit.Framework;
using Rhino.Mocks;
namespace RhinoMockExampleTest
{
[TestFixture]
public class Example1Test
{
[Test]
public void SetParameterTest()
{
var repository = new MockRepository();
var car = repository.StrictMock();
var carMaker = new CarMaker();
using(repository.Record())
{
car.Name = "any";
LastCall.IgnoreArguments();
}
using( repository.Playback())
{
carMaker.Create( car );
}
}
}
public class CarMaker
{
public void Create(ICar car)
{
car.Name = "some name";
}
}
public interface ICar
{
string Name{ get; set;}
string Type { get; set; }
}
}
No comments:
Post a Comment