Fake objects or most commonly known as Mock are widely used to decople your component with it's depency , thus allowing it to be tested independently with the rest of it's dependencies. Let's take a look at a simple code
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
1: public class Patient
2: {
3: public int Save()
4: {
5:
6: // call your database insert statement and return the PatientID
7: return PatientDataAccess.Insert(this);
8: }
9: }
Well you could do Save Test with something like
1: [Test]
2: public void SavePatient()
3: {
4:
5: Patient patient = new Patient();
6: patient.FullName = "erymuzuan";
7: patient.Save();
8:
9: Assert.IsTrue(patient.PatientID > 0);
10: }
It work most of the time, but the big but is what happen if it's not your code where the bug is but your data access code or your database. It would be nice to get rid of the data access code and database dependency from your code