我有以下代码:
public class Weather
{
public int Temperature { get; set; }
public string Description { get; set; }
}
public class WeatherProvider
{
public virtual Weather GetWeather()
{
var w = new Weather();
var hour = Hour;
if (hour < 5)
{
w.Description = "Sunny";
w.Temperature = 12;
}
else if (hour < 12)
{
w.Description = "Windy";
w.Temperature = 18;
}
else
{
w.Description = "Snow";
w.Temperature = 2;
}
return w;
}
public virtual int Hour
{
get { return DateTime.Now.Hour; }
}
}
var mockWeatherProvider = new Moq.Mock<WeatherProvider>(MockBehavior.Loose);
mockWeatherProvider.SetupGet(x => x.Hour).Returns(2);
var actualWeather = mockWeatherProvider.Object.GetWeather();//This line always returns null? WHY???
mockWeatherProvider.Verify(x=>x.GetWeather());
任何人都可以解释为什么行 var actualWeather = mockWeatherProvider.Object.GetWeather() 返回 null 吗?我实际上希望调用真正的方法。我怎样才能做到这一点?