Silverlight的Moq不会引发事件

时间:2010-06-06 19:04:06

标签: silverlight unit-testing silverlight-4.0 moq

尝试使用Moq 4.0.10531.7

为Silverlight 4.0编写单元测试
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);
}

不是提出'DataReceivingEvent',而是收到以下错误:

  

'Castle.Proxies.ISquadModelProxy'类型的对象无法转换为'System.Collections.ObjectModel.ObservableCollection`1 [TeamPlayerData]'类型。

为什么尝试将mock转换为1st事件参数的类型?

我如何举办活动?

我还尝试了另一种方法:

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

如果有人调用'Setup'方法,则应该引发事件...而是生成相同的错误...

欢迎任何想法。

由于

1 个答案:

答案 0 :(得分:2)

发现问题,我需要传递的不是EventArgs.Empty,而是我的所有参数:ObservableCollection AllReadyPlayers,GetSquadDataCompletedEventArgs squadDetails:

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

抱歉无趣的问题。