Wednesday, 18 April 2012

MOQ Samples

MOQ like RhinoMocks uses Castle Dynamic Proxy under the covers to generate “objects” from classes or interfaces.

Here is a quick look at the MOQ Store sample contained within the source: http://code.google.com/p/moq/

Stories

image

Class Diagram

image

Tests

ShouldSetViewCategories

[Test]
public void ShouldSetViewCategories()
{
// Arrange
var catalog = new Mock<ICatalogService>();
var view = new Mock<IProductsView>();

// Act
var presenter = new ProductsPresenter(catalog.Object, view.Object);

// Assert
view.Verify(v => v.SetCategories(It.IsAny<IEnumerable<Category>>()));
}

Shows how Moq generates a real object from an interface, stored in the mock object property
“Verify” extension method from Moq verifies that the expression was called on the mock object
It.IsAny<T>() is generating a default dummy value for the SetCategories, just to check it can be called


ShouldCategorySelectionSetProducts

[Test]
public void ShouldCategorySelectionSetProducts()
{
// Arrange
var catalog = new Mock<ICatalogService>();
var view = new Mock<IProductsView>();
var presenter = new ProductsPresenter(catalog.Object, view.Object);

// Act
view.Raise(
v => v.CategorySelected += null,
new CategoryEventArgs(new Category { Id = 1 }));

// Assert
view.Verify(v => v.SetProducts(It.IsAny<IEnumerable<Product>>()));
}

Shows an event being raised on a mocked object


ShouldPlaceOrderIfEnoughInventory

[Test]
public void ShouldPlaceOrderIfEnoughInventory()
{
// Arrange
var catalog = new Mock<ICatalogService>();
var view = new Mock<IProductsView>();
var presenter = new ProductsPresenter(catalog.Object, view.Object);
var order = new Order
{
Product = new Product { Id = 1 },
Quantity = 5
};

catalog
.Setup(c => c.HasInventory(1, 5))
.Returns(true);

// Act
presenter.PlaceOrder(order);

// Assert
Assert.IsTrue(order.Filled);
catalog.Verify(c => c.HasInventory(1, 5));
}

“Setups” the result of True for the mock object if method HasInventory(1, 5) is called.
NOTE:  presenter.PlaceOrder(order) internally calls HasInventory(1, 5) to set the order.Filled property


ShouldNotPlaceOrderIfNotEnoughInventory

[Test]
public void ShouldNotPlaceOrderIfNotEnoughInventory()
{
// Arrange
var catalog = new Mock<ICatalogService>();
var view = new Mock<IProductsView>();
var presenter = new ProductsPresenter(catalog.Object, view.Object);
var order = new Order
{
Product = new Product { Id = 1 },
Quantity = 5
};

catalog
.Setup(c => c.HasInventory(1, 5))
.Returns(false);

// Act
presenter.PlaceOrder(order);

// Assert
Assert.IsFalse(order.Filled);
catalog.Verify(c => c.HasInventory(1, 5));
}

As per the previous test notes.


ShouldNotPlaceOrderIfFailsToRemove

[Test]
public void ShouldNotPlaceOrderIfFailsToRemove()
{
// Arrange
var catalog = new Mock<ICatalogService>();
var view = new Mock<IProductsView>();
var presenter = new ProductsPresenter(catalog.Object, view.Object);
var order = new Order
{
Product = new Product { Id = 1 },
Quantity = 5
};

catalog
.Setup(c => c.HasInventory(1, 5))
.Returns(true);
catalog
.Setup(c => c.Remove(1, 5))
.Throws<InvalidOperationException>();

// Act
presenter.PlaceOrder(order);

// Assert
Assert.IsFalse(order.Filled);
catalog.Verify(c => c.HasInventory(1, 5));
catalog.Verify(c => c.Remove(1, 5));
}

Source Code


http://stevenhollidge.com/blog-source-code/Moq-StoreSample.zip

No comments:

Post a Comment