I am a "Traditional" mocking guy... I write all my mocks, and got to the point that it has became very fast to implement. But sometimes I just wish that there is a good mocking tool out there.
I have tried Rhino, and I may be stupid (which I am) but I can't figure the damn thing out... I wish there was something that was very straight forward.
NMock2 to the rescue!
I have coded a quick Model View Presenter sample that uses NMock2. Enjoy.
The Test
[TestFixture]public class DJInventoryPresenterTest
{private Mockery mocks;
private DJInventoryPresenter presenter;
private IDJInventoryTask task;
private IDJInventoryView view;
[SetUp]public void SetUp()
{mocks = new Mockery();
task = mocks.NewMock<IDJInventoryTask>(); view = mocks.NewMock<IDJInventoryView>();presenter = new DJInventoryPresenter(view, task);
}
[TearDown]public void TearDown()
{mocks.VerifyAllExpectationsHaveBeenMet();
}
[Test]public void ShouldAdd()
{Expect.Once.On(view).GetProperty("Name");
Expect.Once.On(view).GetProperty("Description");
Expect.Once.On(view).SetProperty("Id");
Expect.Once.On(task).Method("Add").Will(Return.Value(1));
presenter.Add();
}
[Test]public void ShouldDelete()
{Expect.Once.On(view).GetProperty("Name");
Expect.Once.On(view).GetProperty("Description");
Expect.Once.On(view).GetProperty("Id").Will(Return.Value(1));
Expect.Once.On(task).Method("Delete");
presenter.Delete();
}
[Test]public void ShouldUpdate()
{Expect.Once.On(view).GetProperty("Name");
Expect.Once.On(view).GetProperty("Description");
Expect.Once.On(view).GetProperty("Id").Will(Return.Value(1));
Expect.Once.On(view).SetProperty("IsUpdated");
Expect.Once.On(task).Method("Edit").Will(Return.Value(true));
presenter.Update();
}
[Test]public void ShouldGetById()
{Expect.Once.On(view).SetProperty("Name");
Expect.Once.On(view).SetProperty("Description");
Expect.Once.On(view).GetProperty("Id").Will(Return.Value(1));
Expect.Once.On(task).Method("GetDJInventoryById").Will(Return.Value(CreateDJInventory()));
presenter.GetDJInventoryById();
}
[Test]public void ShouldGetAll()
{Expect.Once.On(view).SetProperty("AllInventories");
Expect.Once.On(task).Method("GetAllDJInventories").Will(
Return.Value(new DJInventory[] {CreateDJInventory()}));
presenter.GetAllInventories();
}
private DJInventory CreateDJInventory()
{DJInventory inventory = new DJInventory();
inventory.Name = "Test"; inventory.Description = "Test Description"; return inventory;}
}
The Presenter
public class DJInventoryPresenter
{private readonly IDJInventoryView view;
private readonly IDJInventoryTask task;
public DJInventoryPresenter(IDJInventoryView view, IDJInventoryTask task)
{ this.view = view; this.task = task;}
public void Add()
{DJInventory inventory = CreateDJInventoryFromView(view, true);
view.Id = task.Add(inventory);
}
public void Delete()
{ task.Delete(CreateDJInventoryFromView(view, false));}
public void Update()
{ view.IsUpdated = task.Edit(CreateDJInventoryFromView(view, false));}
public void GetDJInventoryById()
{ DJInventory result = task.GetDJInventoryById(view.Id);view.Name = result.Name;
view.Description = result.Description;
}
private DJInventory CreateDJInventoryFromView(IDJInventoryView view, bool ignoreId)
{DJInventory inventory = new DJInventory();
inventory.Name = view.Name;
inventory.Description = view.Description;
if (!ignoreId)inventory.Id = view.Id;
return inventory;}
public void GetAllInventories()
{view.AllInventories = CreateDataTable(task.GetAllDJInventories());
}
private DataTable CreateDataTable(DJInventory[] inventories)
{DataTable dt = new DataTable();
CreateColumns(dt);
CreateRows(dt, inventories);
return dt;}
private void CreateColumns(DataTable dt)
{ dt.Columns.Add("ID"); dt.Columns.Add("Name"); dt.Columns.Add("Description");}
private void CreateRows(DataTable dt, DJInventory[] inventories)
{foreach (DJInventory inventory in inventories)
{dt.Rows.Add(CreateRow(dt, inventory));
}
}
private DataRow CreateRow(DataTable dt, DJInventory inventory)
{ DataRow row = dt.NewRow(); row["ID"] = inventory.Id; row["Name"] = inventory.Name; row["Description"] = inventory.Description; return row;}
}

4 comments:
Awesome Man! I use nmock2 at work and have found it to be a great way to mock out the different layers (domain, proxy, task etc.)
Too bad the project on sourceforge suffers from poor management - it could be the one thing that may kill it.
Nmock2 is pretty pimp... I have started the move to get used to using nmock2... oooh soo good.
Oh yea, as long as they have the same build in SF, i dont care coz it just works!
If you are serious about testing and keeping the tests readable you should get your employer to get TypeMock.NET.
It just makes this soo much easier.
I have take a look at typemock before. No offense but, I would not want to pay for a mocking tool. There are soo much alternatives out there that are opensource / free, and we are not there yet when it comes to TDD and mocking.
Post a Comment