尝试使用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'方法,则应该引发事件...而是生成相同的错误...
欢迎任何想法。
由于
答案 0 :(得分:2)
发现问题,我需要传递的不是EventArgs.Empty,而是我的所有参数:ObservableCollection AllReadyPlayers,GetSquadDataCompletedEventArgs squadDetails:
mockSquadModel
.Setup(model => model.RequestData(TestMatchId, TestTeamId))
.Raises(model => model.DataReceivedEvent += null, players, squadDetails);
;
抱歉无趣的问题。