I
Ilyas
Hi all
Not sure if this is the right group, I am happyo to repost to correct
group if I know what that is! but in the mean time - here goes
I am using NMock2 and I have question.
I have the following interface:
public interface ITime
{
int Hour { get; }
}
and the following weather service:
public class WeatherService:ITime
{
public string GetTodaysWeather()
{
if (Hour >= 0 && Hour <= 6)
{
return "Sunny";
}
else if (Hour >= 7 && Hour <= 15)
{
return "Foggy";
}
else
{
return "Snowing";
}
}
public int Hour
{
get { return DateTime.Now.Hour; }
}
}
In order for me to test this I have created the simple test:
[Test]
public void CanGetSunnyWeather()
{
Mockery m = new Mockery();
ITime mockObject = m.NewMock<ITime>();
Stub.On(mockObject).GetProperty("Hour").Will(Return.Value(2));
WeatherService ws = new WeatherService();
Assert.AreEqual("Sunny", ws.GetTodaysWeather());
m.VerifyAllExpectationsHaveBeenMet();
}
This fails as the Hour Property on Weather service doesnt get mocked
and I expected it to. Why is this? I want to be able to test this
weather servce without relying on the time which is why I mocked it -
but it doesnt behave as expected any ideas anyone?
Not sure if this is the right group, I am happyo to repost to correct
group if I know what that is! but in the mean time - here goes
I am using NMock2 and I have question.
I have the following interface:
public interface ITime
{
int Hour { get; }
}
and the following weather service:
public class WeatherService:ITime
{
public string GetTodaysWeather()
{
if (Hour >= 0 && Hour <= 6)
{
return "Sunny";
}
else if (Hour >= 7 && Hour <= 15)
{
return "Foggy";
}
else
{
return "Snowing";
}
}
public int Hour
{
get { return DateTime.Now.Hour; }
}
}
In order for me to test this I have created the simple test:
[Test]
public void CanGetSunnyWeather()
{
Mockery m = new Mockery();
ITime mockObject = m.NewMock<ITime>();
Stub.On(mockObject).GetProperty("Hour").Will(Return.Value(2));
WeatherService ws = new WeatherService();
Assert.AreEqual("Sunny", ws.GetTodaysWeather());
m.VerifyAllExpectationsHaveBeenMet();
}
This fails as the Hour Property on Weather service doesnt get mocked
and I expected it to. Why is this? I want to be able to test this
weather servce without relying on the time which is why I mocked it -
but it doesnt behave as expected any ideas anyone?