I m using NUnit, I have following code which will be tested.
public class StudentPresenter
{
IView myview;
Repository myrepo;
public StudentPresenter(IView vw, Data.Repository rep)
{
this.myview = vw;
this.myrepo = rep;
this.myview.ButtonClick += myview_ButtonClick;
}
public void myview_ButtonClick()
{
try
{
var d = this.myrepo.GetById(int.Parse(myview.GivenId));
this.myview.GetStudent(d);
}
catch(Exception e)
{
}
}
}
I need to test myview_ButonClick()
Lets suppose I will test this method and it will throw exception if myview.GivenId
is null?
So I write unit test as below:
[Test]
public void Button1Click_NullText_ThrowException()
{
var mock = Substitute.For<IView>();
StudentPresenter sp = new StudentPresenter(mock, repo);
mock.GivenId = string.Empty;
Assert.Throws<Exception>(()=>sp.myview_ButtonClick());
}
But test failed.. Why? (becouse no throw in my catch block). But I dont want to throw anything, I just want that it has to ability to catch. So is it possible to test?