chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
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
asked by andreister (3.4k points)

2 Answers

0 votes
Hi Andrew

So far Event support was not added to AAA syntax.
regarding the test below, heres another way to do this (i don't know if its better but it is different)
[Test, Isolated]
public void Test2_MockedEvent()
{
    var warehouse = Isolate.Fake.Instance<IWarehouse>();
    Isolate.WhenCalled(() => warehouse.GetProducts(null)).WillReturn(DefaultProducts);

    //still no support for events in AAA so need to resort to older syntax
    Mock mock = MockManager.GetMockOf<IWarehouse>(warehouse);
    MockedEvent handle = mock.ExpectAddEvent("SomethingWentWrong");
    mock.MethodSettings("GetProducts").MockMethodCalled += 
        new MockMethodCalledEventHandler((x, y) => handle.Fire(x, new WarehouseEventArgs { BadRequest = true }));


    var cart = new ShoppingCart();
    cart.AddProducts("foo", warehouse);

    Assert.IsTrue(cart.IsRed, "Cart should go to the 'red' state if there was a bad request to the wharehouse.");

}


:?: I really liked your comparison project, any plans on adding more examples there?
(If you do and you would like some help let me know)
answered by error (6.6k points)
0 votes
Hi Lior,

Thanks for your sample - very, very interesting! Though I feel mine is more descriptive but I'm definitely biased :)

As for the project - there are updates in svn (by now, all mocking frameworks, including Isolator, have the same set of tests). I will add an updated zip shortly, just need a bit more work on other frameworks.


For others who is out of the picture.

There's a project http://code.google.com/p/mocking-frameworks-compare/ that covers the same set of test scenarios in Isolator, Moq, NMock2 and Rhino Mocks (including source code of the frameworks, if available). So you can compare syntaxes, debug, learn, etc, etc.

answered by andreister (3.4k points)
...