Below is some code which is just the simplest MVP project I could make. How can I unit test the presenter using mock objects? Does a mock object mean a completely made up object? In this case, the presenter needs an instance of Form1, and Form1 inherits from the interface IForm1. Does that mean I need to create fake duplicates of Form1 and IForm1 that are used to unit test my presenter?
I feel like I'm really close to understanding what's going on here, I really want to get my head around unit testing an MVP model, I think it will be greatly beneficial for the system I'm working on.
Thanks!
using System;
using System.Windows.Forms;
namespace MVP_Testing {
public partial class Form1 : Form, IForm1 {
private Presenter p;
public Form1() {
InitializeComponent();
p = new Presenter(this);
}
#region Interface
public string Value1 {
get { return inputValue1.Text; }
}
public string Value2 {
get { return inputValue2.Text; }
}
public string Result {
set { textBoxResult.Text = value; }
}
#endregion
private void buttonCalculate_Click(object sender, EventArgs e) {
p.DoCalculation();
}
}
public interface IForm1 {
string Value1 { get; }
string Value2 { get; }
string Result { set; }
}
public class Presenter {
private readonly IForm1 _iform1;
public Presenter(IForm1 iform1) {
_iform1 = iform1;
}
public void DoCalculation() {
int value1 = Convert.ToInt16(_iform1.Value1);
int value2 = Convert.ToInt16(_iform1.Value2);
_iform1.Result = (value1 * value2).ToString();
}
}
}