Labs Hands On / NUNIT - MockView

Topics: CAB & Smart Client Software Factory
Jan 2, 2006 at 1:43 AM
originally posted by: yarezki

I went through Lab 1 and everything worked. I wanted to test GPSView, So I created an interface called IGPS as follows:

public interface IGPS
{
string Latitude { get;set;}
string Distance { get;set;}
}

I then got the GPSView to implement IGPS and I also created GPSViewController so that I could test using a GPSMockView.

public partial class GPSView : UserControl,IGPS
{

private GPSController controller;
public GPSView()
{
InitializeComponent();

}

private void cmdGetDistance_Click(object sender, EventArgs e)
{
// Note that we didn't added a ServiceDependencyAttribute for the
// IDistanceCalculatorService, because that way the service would be created
// when this view is added to the work item. This is just for the purposes of this example.
//The service is not loaded into memory until this method is called.
// The ServiceDependency is not applied until the property is called the first time

//TODO: Use IDistanceCalculatorService here
controller.CalculateDistance();
}

private void cmdGetLatitude_Click(object sender, EventArgs e)
{
//TODO: Paste GetLatitude code here
controller.CalculateLatitude();
}

#region IGPS Members

public string Latitude
{
get { return txtLatitude.Text;}
set { txtLatitude.Text = value;}
}

public string Distance
{
get { return txtDistance.Text; }
set { txtDistance.Text = value; }
}

#endregion

private void GPSView_Load(object sender, EventArgs e)
{
controller = new GPSController(this);
}
}

// GPSViewController

public class GPSController
{
private IGPS _view;
private WorkItem parentWorkItem;

ServiceDependency
public WorkItem ParentWorkItem
{
set { parentWorkItem = value; }
}
//TODO:
private IGPSService gpsService;
ServiceDependency
public IGPSService GPS
{
set { gpsService = value; }
}
public GPSController(IGPS view)
{
_view = view;
}
public void CalculateDistance()
{
IDistanceCalculatorService calc = parentWorkItem.Services.Get<IDistanceCalculatorService>();
_view.Distance = calc.ComputeDistance(gpsService.GetLatitude(), gpsService.GetLongitude()).ToString();

}
public void CalculateLatitude()
{
_view.Latitude = gpsService.GetLatitude().ToString();
}

I compiled and it compiles but when I run the application and click on the GetLatitude, gpsService is null and similarly when I click on GetDistance. Could somebody shed some light on this small problem.

Thanks
Yazid
Jan 4, 2006 at 3:19 AM
originally posted by: yarezki

Hello again,

Peter Provost, I am sure you could answer this, I have only tried to mimic one of the articles you had published on dependency Inversion Principle.

Thanks
Jan 5, 2006 at 4:53 PM
originally posted by: yarezki

in GPSView create the controller as follows:

private GPSController controller;

CreateNew
public GPSController Controller
{
set { controller = value; controller.View = this; }
}

private void GPSView_Load(object sender, EventArgs e)
{
// controller = new controller(this);
}
in the controller:

public class GPSController
{
private IGPS _view;
public IGPS View
{
set { _view = value; }
}

private WorkItem parentWorkItem;

ServiceDependency
public WorkItem ParentWorkItem
{
get { return parentWorkItem; }
set { parentWorkItem = value; }
}
//TODO:
private IGPSService gpsService;
ServiceDependency
public IGPSService GPS
{
get { return gpsService; }
set { gpsService = value; }
}

public void CalculateDistance()
{
//IDistanceCalculatorService calc = parentWorkItem.Services.Get<IDistanceCalculatorService>();
//_view.Distance = calc.ComputeDistance(gpsService.GetLatitude(), gpsService.GetLongitude()).ToString();

IDistanceCalculatorService calc = parentWorkItem.Services.Get<IDistanceCalculatorService>();
_view.Distance = calc.ComputeDistance(gpsService.GetLatitude(), gpsService.GetLongitude()).ToString();

}
public void CalculateLatitude()
{
_view.Latitude = gpsService.GetLatitude().ToString();
}
}

and it works like a treat and thus we have moved all our business logic to the controller and hence we could now test GPSView using a mockView.