나는의 WinForm입니다 볼 수 있습니다C#, Nunit, Moq 발표자가 뷰 이벤트에 가입했는지 테스트하는 방법은 무엇입니까?
public partial class View : Form, IView
{
private static object eventsLock = new Object();
private EventHandler CustomClick;
public View()
{
InitializeComponent();
this.button.Click += FireCustomClickEvent;
}
event EventHandler IView.Click
{
add { lock (eventsLock) { this.CustomClick += value; }}
remove { lock (eventsLock) { this.CustomClick -= value; }}
}
private void FireCustomClickEvent(object sender, EventArgs e)
{
this.CustomClick?.Invoke(sender, e);
}
public void MakeViewDoStuff()
{
//Do stuff...
}
}
IView
인터페이스는 다음과 같다 :
public interface IView
{
event Eventhandler Click;
void MakeViewDoStuff();
}
그리고 나는이 발표자가 :
public class Presenter : IPresenter
{
private IView view;
public Presenter(IView view)
{
this.view = view;
this.AttachView();
}
private void AttachView()
{
this.view.Click += SomePresenterStuff;
}
private void SomePresenterStuff(object sender EventArgs e)
{
this.view.MakeViewDoStuff();
//Do stuff now that the event was raised.
}
}
지금 나는 그것을 테스트 할 수 있습니다 이벤트가 발생하고 발표자가 뷰를이 테스트 기능으로 처리하게 만듭니다.
this.mockedView.Raise(v => v.Click += null, new System.EventArgs());
this.mockedView.Verify(v => v.MakeViewDoStuff(), Times.Once());
하지만 내가 알고 싶은 것은 : 발표자가 생성자에 첨부되어 있는지 테스트해야합니까?
기본적으로 Presenter의 생성자에서 private void AttachView()
이 호출되는지 테스트해야하며 어떻게해야합니까?
일부 노트 : 나는 부울 플래그를 추가하는 것은 발표자의 부착 상태를 유지 할 수 있다고 생각
은 단지 그것을 테스트 할 수 있도록 생산 코드를 변경 포함되지 것 것이기 때문에 나쁜 해킹의 일종이다 생산에 사용된다.
편집 :
내가 가지가 발표자 Responsibility는 나 행동하지만,되지 않을 수도 세르게이 Berezovskiy에 동의내보기는 CloseView 이벤트가 있다면 제기 때, 발표자에 탈퇴해야하는지 뷰 이벤트. 이걸 시험하고 싶지 않니? 누가 발표자가 이벤트에 부착 된 방법 신경 -
조롱 한보기에서 이것을 확인하고 싶지만 [Moq은 여전히 이벤트 등록을 확인할 수 없습니다.] (https://github.com/moq/moq4/issues/49) – stuartd
@stuartd이 링크는 매우 usefull – Sebastien