| Der Frühling kommt! |
| die Knospen sprießen! |
![]() |
| Turn up the fun: Das mach ich jetzt :) |
![]() |
| Neues Feature im Nokia Lumia 610: Die "Bierdeckel" Funktion! |
public static class DateTimeHelper
{
///
/// Returns an text according to the current time
///
///
///
/// Uhrzeiten die unterschieden werden sollen:
///
/// - 23-6: night
/// - 7-10: morning
/// - 11-13: noon
/// - 14-22: evening
///
///
public static string GetTextForCurrentDate()
{
// Dependency to the DateTime Object
// Will always return the current time
DateTime currentTime = DateTime.Now;
if (currentTime.Hour >= 23 || currentTime.Hour <= 6)
{
return "night";
}
if (currentTime.Hour >= 7 && currentTime.Hour <= 10)
{
return "morning";
}
if (currentTime.Hour >= 11 && currentTime.Hour <= 13)
{
return "noon";
}
if (currentTime.Hour >= 14 && currentTime.Hour <= 22)
{
return "evening";
}
throw new NotImplementedException("This sould never happen");
}
}
But wait! There is Typemock Isolator a tool that let's you fake / mock any dependency! With Typemock we are also able to take control over the DateTime object!
[TestClass, Isolated(DesignMode.Pragmatic)]
public class DateTimeHelperTest
{
private TestContext testContextInstance;
public TestContext TestContext
{
get { return testContextInstance; }
set { testContextInstance = value; }
}
///
/// A test for GetImageForCurrentTime
///
[TestMethod()]
[DeploymentItem("BusinessLogic.Test\\TestData\\GetImageForCurrentTime_TestBoundaries.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\GetImageForCurrentTime_TestBoundaries.xml", "Row", DataAccessMethod.Sequential)]
public void GetTextForCurrentDateTest()
{
int hourToTest = Int32.Parse((string)TestContext.DataRow["HourToTest"]);
string assertValue = (string)TestContext.DataRow["AssertValue"];
Execute_GetTextForCurrentDateTest(hourToTest, assertValue);
}
private void Execute_GetTextForCurrentDateTest(int hourToTest, string assertValue)
{
string expected = assertValue;
// If DateTime.Now is called, our DateTime wil be returned
DateTime controlledDate = new DateTime(2013, 1, 1, hourToTest, 0, 0);
// This is very the Typemock magic happens!
Isolate.WhenCalled(() => DateTime.Now).WillReturn(controlledDate);
string actual = DateTimeHelper.GetTextForCurrentDate();
Assert.AreEqual(expected, actual);
}
}
This is the XML File with all the neccessary test data. In my opinion it is the easiest way to simply test any hour that can occur.
<?xml version="1.0" encoding="utf-8" ?>
<Rows>
<Row>
<HourToTest>0</HourToTest>
<AssertValue>night</AssertValue>
</Row>
<Row>
<HourToTest>1</HourToTest>
<AssertValue>night</AssertValue>
</Row>
<Row>
<HourToTest>2</HourToTest>
<AssertValue>night</AssertValue>
</Row>
<Row>
<HourToTest>3</HourToTest>
<AssertValue>night</AssertValue>
</Row>
<Row>
<HourToTest>4</HourToTest>
<AssertValue>night</AssertValue>
</Row>
<Row>
<HourToTest>5</HourToTest>
<AssertValue>night</AssertValue>
</Row>
<Row>
<HourToTest>6</HourToTest>
<AssertValue>night</AssertValue>
</Row>
<Row>
<HourToTest>7</HourToTest>
<AssertValue>morning</AssertValue>
</Row>
<Row>
<HourToTest>8</HourToTest>
<AssertValue>morning</AssertValue>
</Row>
<Row>
<HourToTest>9</HourToTest>
<AssertValue>morning</AssertValue>
</Row>
<Row>
<HourToTest>10</HourToTest>
<AssertValue>morning</AssertValue>
</Row>
<Row>
<HourToTest>11</HourToTest>
<AssertValue>noon</AssertValue>
</Row>
<Row>
<HourToTest>12</HourToTest>
<AssertValue>noon</AssertValue>
</Row>
<Row>
<HourToTest>13</HourToTest>
<AssertValue>noon</AssertValue>
</Row>
<Row>
<HourToTest>14</HourToTest>
<AssertValue>evening</AssertValue>
</Row>
<Row>
<HourToTest>15</HourToTest>
<AssertValue>evening</AssertValue>
</Row>
<Row>
<HourToTest>16</HourToTest>
<AssertValue>evening</AssertValue>
</Row>
<Row>
<HourToTest>17</HourToTest>
<AssertValue>evening</AssertValue>
</Row>
<Row>
<HourToTest>18</HourToTest>
<AssertValue>evening</AssertValue>
</Row>
<Row>
<HourToTest>19</HourToTest>
<AssertValue>evening</AssertValue>
</Row>
<Row>
<HourToTest>20</HourToTest>
<AssertValue>evening</AssertValue>
</Row>
<Row>
<HourToTest>21</HourToTest>
<AssertValue>evening</AssertValue>
</Row>
<Row>
<HourToTest>22</HourToTest>
<AssertValue>evening</AssertValue>
</Row>
<Row>
<HourToTest>23</HourToTest>
<AssertValue>night</AssertValue>
</Row>
</Rows>
<?xml version="1.0" encoding="utf-8" ?>
<Rows>
<Row>
<Country></Country>
<Image>unknown.gif</Image>
</Row>
<Row>
<Country>germany</Country>
<Image>germany.gif</Image>
</Row>
<Row>
<Country>usa</Country>
<Image>usa.gif</Image>
</Row>
<Row>
<Country>some country</Country>
<Image>unknown.gif</Image>
</Row>
</Rows>
Unit Test:
[TestClass]
public class CountryHandlerTest
{
private TestContext testContextInstance;
public TestContext TestContext
{
get { return testContextInstance; }
set { testContextInstance = value; }
}
[TestMethod]
[DeploymentItem("BusinessLogic.Test\\TestData\\GetImageForCountry.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\GetImageForCountry.xml", "Row", DataAccessMethod.Sequential)]
public void GetImageForCountry_SeveralTests()
{
// Get data out of the XML File
string country = testContextInstance.DataRow["Country"].ToString();
string image = testContextInstance.DataRow["Image"].ToString();
Execute_GetImageForCountry_SeveralTests(country, image);
}
///
/// Helper Method that executes the test
///
///
///
private void Execute_GetImageForCountry_SeveralTests(string country, string image)
{
#region Arrange
string expected = image;
CountryHandler target = new CountryHandler();
#endregion
#region Act
string actual = target.GetImageForCountry(country);
#endregion
#region Assert
Assert.AreEqual(expected, actual);
#endregion
}
}
Method that will be tested:
public class CountryHandler
{
///
/// Get the Image for a given country
///
///
/// The following requirements must be fulfilled:
///
/// - "germany" return "germany.gif"
/// - "usa" return "usa.gif"
/// - all other countries return "unknown.gif"
///
///
///
///
public string GetImageForCountry(string country)
{
if (country == "germany")
{
return "germany.gif";
}
if (country == "usa")
{
return "usa.gif";
}
return "unknown.gif";
}
}
Runnning the test gives me a nice code coverage ;)C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Binwsdl.exe /language:CS /namespace:EnterpriseBugs.FakeWebservice.FakeService /protocol:SOAP /out:C:\projects\enterprisebugs.com-samples\FakeWebservice\FakeService\CountryInformationServiceInterfaces.cs /serverinterface http://www.ezzylearning.com/services/CountryInformationService.asmx
private static DataSet PopulateDataSet()
{
DataSet ds = new DataSet();
#region Countries
DataTable dtCountries = new DataTable("Countries");
dtCountries.Columns.Add(new DataColumn("ISO2", typeof(string)));
dtCountries.Columns.Add(new DataColumn("ISO3", typeof(string)));
dtCountries.Columns.Add(new DataColumn("Country", typeof(string)));
DataRow drCountries = dtCountries.NewRow();
drCountries["ISO2"] = "AX";
drCountries["ISO3"] = "ALA";
drCountries["Country"] = "Aland Islands";
dtCountries.Rows.Add(drCountries);
drCountries = dtCountries.NewRow();
drCountries["ISO2"] = "CN";
drCountries["ISO3"] = "CHN";
drCountries["Country"] = "China";
dtCountries.Rows.Add(drCountries);
drCountries = dtCountries.NewRow();
drCountries["ISO2"] = "DE";
drCountries["ISO3"] = "DEU";
drCountries["Country"] = "Germany";
dtCountries.Rows.Add(drCountries);
drCountries = dtCountries.NewRow();
drCountries["ISO2"] = "FR";
drCountries["ISO3"] = "FRA";
drCountries["Country"] = "France";
dtCountries.Rows.Add(drCountries);
ds.Tables.Add(dtCountries);
#endregion
return ds;
}
And then call PopulateDataSet() inside the GetCountries()-method:
public System.Data.DataSet GetCountries()
{
return PopulateDataSet();
}
We are done with the coding issues. Let's start the webservice




static void Main(string[] args)
{
// get a instance of the webservice
CountryInformationService service = new CountryInformationService();
// call the method
string result = service.GetPopulationByCountry("Germany");
Console.WriteLine("Germany has a population of: " + result);
Console.ReadLine();
}

| Feature Name | Depends On |
|---|---|
| Feature1 (da5198d4-b81e-4098-9799-d23641c558c2) | none |
| Feature2 depends on Feature1 (42f35cbc-e46d-4cdd-88d3-a3e7f83ee31a) | Feature1 |
| Feature3 depends on Feature2 (eb1d232d-8e05-4563-85ac-c0c8ff2919e8) | Feature2 depends on Feature1 |