在我的代码中:
public class StudentPresenter
{
IView myview;
Repository myrepo;
public StudentPresenter(IView vw, Data.Repository rep)
{
this.myview = vw;
this.myrepo = rep;
this.myview.ButonClick += myview_ButonClick;
}
public void myview_ButonClick()
{
var d = this.myrepo.GetById(int.Parse(myview.GivenId));
this.myview.GetStudent(d);
}
}
我想测试GetStudent
方法会调用,所以我尝试了
[Test]
public void ctor_whenviewbuttonclick_callsviewButtonClickevent()
{
var mock = Substitute.For<IView>();
var stub=Substitute.For<Repository>();
Student st = new Student();
stub.When(p => p.GetById(Arg.Any<int>())).Do(x => st=new Student());
StudentPresenter sp = new StudentPresenter(mock, stub);
mock.ButonClick += Raise.Event<Action>();
mock.Received().GetStudent(st);
}
但测试坏了: 说:
Application.UnitTests.Form1Tests.ctor_whenviewbuttonclick_callsviewButtonClickevent: NSubstitute.Exceptions.ReceivedCallsException : 预期会收到匹配的呼叫:GetStudent(Student) 实际上没有收到匹配的呼叫。
我在这里做错了什么?