.NET Core 中 Microsoft Fakes 的任何替代方案?
Any alternative for Microsoft Fakes in .NET Core?
我正在寻找 .NET Core 中 Microsoft Fakes 的替代品。我知道 .NET Core 不再支持它。我只是不明白为什么不,我认为在某些情况下这是一个很好的解决方案。
我的问题是我想模拟 DateTime.Now。以前您可以使用以下代码执行此操作:
System.Fakes.ShimDateTime.NowGet = () =>
{
return new DateTime(2000, 1, 1);
};
在微软文档中有描述,详见link:https://docs.microsoft.com/en-us/visualstudio/test/using-shims-to-isolate-your-application-from-other-assemblies-for-unit-testing?view=vs-2017
现在我通过为 DateTime 创建一个包装器解决了这个问题,它看起来像这样:
/// <summary>
/// Used for getting DateTime.Now(), time is changeable for unit testing
/// </summary>
public static class SystemTime
{
/// <summary>
/// Normally this is a pass-through to DateTime.Now, but it can be
/// overridden with SetDateTime( .. ) for testing or debugging.
/// </summary>
public static Func<DateTime> Now = () => DateTime.Now;
/// <summary>
/// Set time to return when SystemTime.Now() is called.
/// </summary>
public static void SetDateTime(DateTime dateTimeNow)
{
Now = () => dateTimeNow;
}
/// <summary>
/// Resets SystemTime.Now() to return DateTime.Now.
/// </summary>
public static void ResetDateTime()
{
Now = () => DateTime.Now;
}
}
这个解决方案归功于下一个 Whosebug post:
Unit Testing: DateTime.Now
但我对这个解决方案还不满意,因为我觉得我必须为我的测试调整我的实现。我认为这是不可取的。
我希望有人能帮我解决这个问题,在此先感谢您的努力。
感谢所有评论,这对我很有帮助。我稍微修改了我的实现;
SystemTime class 现在称为 DateTimeProvider,如下所示:
/// <summary>
/// Used for getting DateTime.Now(), time is changeable for unit testing
/// </summary>
public class DateTimeProvider : IDateTimeProvider
{
/// <summary>
/// Normally this is a pass-through to DateTime.Now, but it can be
/// overridden with SetDateTime( .. ) for testing or debugging.
/// </summary>
private Func<DateTime> _now = () => DateTime.Now;
public Func<DateTime> Now { get => _now; private set => _now = value; }
/// <summary>
/// Set time to return when DateTimeProvider.Now() is called.
/// </summary>
public void SetDateTime(DateTime dateTimeNow)
{
Now = () => dateTimeNow;
}
/// <summary>
/// Resets DateTimeProvider.Now() to return DateTime.Now.
/// </summary>
public void ResetDateTime()
{
Now = () => DateTime.Now;
}
}
我选择将 setter 设为 Now() 的私有。因此,开发人员必须显式使用 SetDateTime() 方法来更改时间。您还可以选择使用自动 getter 和 setter 属性.
我还添加了一个接口,以便 class 可以被注入:
public interface IDateTimeProvider
{
Func<DateTime> Now { get; }
void SetDateTime(DateTime dateTimeNow);
void ResetDateTime();
}
我希望其他人也能从中受益。
Pose 对此效果很好。
using Pose;
Shim dateTimeShim = Shim.Replace(() => DateTime.Now).With(() => new DateTime(2004, 4, 4));
// This block executes immediately
PoseContext.Isolate(() =>
{
// All code that executes within this block
// is isolated and shimmed methods are replaced
// Outputs "4/4/04 12:00:00 AM"
Console.WriteLine(DateTime.Now);
}, dateTimeShim);
I am looking for an alternative to Microsoft Fakes in .NET Core. I
know it is no longer supported in .NET Core. I just do not understand
why not, I think it was a good solution in certain situations.
自 2020 年 5 月 19 日起 Microsoft Fakes 支持 .NET Core。
https://docs.microsoft.com/en-us/visualstudio/releases/2019/release-notes#16.6.0
我正在寻找 .NET Core 中 Microsoft Fakes 的替代品。我知道 .NET Core 不再支持它。我只是不明白为什么不,我认为在某些情况下这是一个很好的解决方案。
我的问题是我想模拟 DateTime.Now。以前您可以使用以下代码执行此操作:
System.Fakes.ShimDateTime.NowGet = () =>
{
return new DateTime(2000, 1, 1);
};
在微软文档中有描述,详见link:https://docs.microsoft.com/en-us/visualstudio/test/using-shims-to-isolate-your-application-from-other-assemblies-for-unit-testing?view=vs-2017
现在我通过为 DateTime 创建一个包装器解决了这个问题,它看起来像这样:
/// <summary>
/// Used for getting DateTime.Now(), time is changeable for unit testing
/// </summary>
public static class SystemTime
{
/// <summary>
/// Normally this is a pass-through to DateTime.Now, but it can be
/// overridden with SetDateTime( .. ) for testing or debugging.
/// </summary>
public static Func<DateTime> Now = () => DateTime.Now;
/// <summary>
/// Set time to return when SystemTime.Now() is called.
/// </summary>
public static void SetDateTime(DateTime dateTimeNow)
{
Now = () => dateTimeNow;
}
/// <summary>
/// Resets SystemTime.Now() to return DateTime.Now.
/// </summary>
public static void ResetDateTime()
{
Now = () => DateTime.Now;
}
}
这个解决方案归功于下一个 Whosebug post: Unit Testing: DateTime.Now
但我对这个解决方案还不满意,因为我觉得我必须为我的测试调整我的实现。我认为这是不可取的。
我希望有人能帮我解决这个问题,在此先感谢您的努力。
感谢所有评论,这对我很有帮助。我稍微修改了我的实现;
SystemTime class 现在称为 DateTimeProvider,如下所示:
/// <summary>
/// Used for getting DateTime.Now(), time is changeable for unit testing
/// </summary>
public class DateTimeProvider : IDateTimeProvider
{
/// <summary>
/// Normally this is a pass-through to DateTime.Now, but it can be
/// overridden with SetDateTime( .. ) for testing or debugging.
/// </summary>
private Func<DateTime> _now = () => DateTime.Now;
public Func<DateTime> Now { get => _now; private set => _now = value; }
/// <summary>
/// Set time to return when DateTimeProvider.Now() is called.
/// </summary>
public void SetDateTime(DateTime dateTimeNow)
{
Now = () => dateTimeNow;
}
/// <summary>
/// Resets DateTimeProvider.Now() to return DateTime.Now.
/// </summary>
public void ResetDateTime()
{
Now = () => DateTime.Now;
}
}
我选择将 setter 设为 Now() 的私有。因此,开发人员必须显式使用 SetDateTime() 方法来更改时间。您还可以选择使用自动 getter 和 setter 属性.
我还添加了一个接口,以便 class 可以被注入:
public interface IDateTimeProvider
{
Func<DateTime> Now { get; }
void SetDateTime(DateTime dateTimeNow);
void ResetDateTime();
}
我希望其他人也能从中受益。
Pose 对此效果很好。
using Pose;
Shim dateTimeShim = Shim.Replace(() => DateTime.Now).With(() => new DateTime(2004, 4, 4));
// This block executes immediately
PoseContext.Isolate(() =>
{
// All code that executes within this block
// is isolated and shimmed methods are replaced
// Outputs "4/4/04 12:00:00 AM"
Console.WriteLine(DateTime.Now);
}, dateTimeShim);
I am looking for an alternative to Microsoft Fakes in .NET Core. I know it is no longer supported in .NET Core. I just do not understand why not, I think it was a good solution in certain situations.
自 2020 年 5 月 19 日起 Microsoft Fakes 支持 .NET Core。
https://docs.microsoft.com/en-us/visualstudio/releases/2019/release-notes#16.6.0