Moq for Silverlight doesn't raise event

Posted by Budda on Stack Overflow See other posts from Stack Overflow or by Budda
Published on 2010-06-06T19:04:05Z Indexed on 2010/06/06 19:22 UTC
Read the original article Hit count: 556

Trying to write Unit test for Silverlight 4.0 using Moq 4.0.10531.7

public delegate void DataReceived(ObservableCollection<TeamPlayerData> AllReadyPlayers, GetSquadDataCompletedEventArgs squadDetails);

public interface ISquadModel : IModelBase
{
    void RequestData(int matchId, int teamId);
    void SaveData();

    event DataReceived DataReceivedEvent;
}

void MyTest()
{
    Mock<ISquadModel> mockSquadModel = new Mock<ISquadModel>();
    mockSquadModel.Raise(model => model.DataReceivedEvent += null, EventArgs.Empty);
}

Instead of raising the 'DataReceivingEvent' the following error is received:

Object of type 'Castle.Proxies.ISquadModelProxy' cannot be converted to type 'System.Collections.ObjectModel.ObservableCollection`1[TeamPlayerData]'.

Why attempt to convert mock to the type of 1st event parameter is performed?

How can I raise an event?

I've also tried another approach:

mockSquadModel
            .Setup(model => model.RequestData(TestMatchId, TestTeamId))
            .Raises(model => model.DataReceivedEvent += null, EventArgs.Empty)
            ;

this should raise event if case somebody calls 'Setup' method... Instead the same error is generated...

Any thoughts are welcome.

Thanks

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about unit-testing