Hi here's my ShoppingCart
public virtual void AddProducts(string name, IWarehouse warehouse)
{
try {
warehouse.SomethingWentWrong += Alarm;
var products = warehouse.GetProducts(name);
products.ForEach(p => _products.Add(p));
}
finally {
warehouse.SomethingWentWrong -= Alarm;
}
}
...
public void Alarm(object sender, WarehouseEventArgs args)
{
if (args.BadRequest)
IsRed = true;
}
I need to mock the warehouse and test that if warehouse fires "SomethingWentWrong" then the cart's IsRed becomes true.
As far as I understand, it's
not possible to do that in AAA - is that correct? If yes, is there a plan to add event support to AAA?
Here's the test I've created
[Test, Isolated]
public void SomethingWentWrong_IsRed()
{
var warehouse = MockManager.MockObject<IWarehouse>();
var alarm = warehouse.ExpectAddEvent("SomethingWentWrong");
warehouse.ExpectRemoveEvent("SomethingWentWrong");
warehouse.ExpectAndReturn("GetProducts", FireAndReturn(alarm, DefaultProducts));
var cart = new ShoppingCart();
cart.AddProducts("foo", warehouse.MockedInstance);
Assert.IsTrue(cart.IsRed, "Cart should go to the 'red' state if there was a bad request to the wharehouse.");
}
...
private static DynamicReturnValue FireAndReturn(MockedEvent alarm, List<Product> result)
{
return (args, obj) => {
alarm.Fire(null, new WarehouseEventArgs { BadRequest = true });
return result;
};
}
I wonder if there's a better way.
Thanks,
Andrew