gpt4 book ai didi

unit-testing - 来自 commonlibnet 的 FakeItEasy 和 FluentValidation 的假验证码

转载 作者:行者123 更新时间:2023-12-01 15:01:34 27 4
gpt4 key购买 nike

我正在使用来自 commonlibrary (http://commonlibrarynet.codeplex.com/) 的 Captcha 类。我的代码可以正常工作,但现在我正在尝试编写单元测试。

我的验证规则是:

 RuleFor(x => x.CaptchaUserInput)
.NotEmpty()
.Must((x, captchaUserInput) => Captcha.IsCorrect(captchaUserInput, x.CaptchaGeneratedText))
.WithMessage("Invalid captcha code");

在我的设置代码中,我尝试执行以下操作:

A.CallTo(() => Captcha.IsCorrect()).Returns(true);

但我收到以下错误消息:

SetUp : FakeItEasy.Configuration.FakeConfigurationException : 

The current proxy generator can not intercept the specified method for the following reason:
- Static methods can not be intercepted.


at FakeItEasy.Configuration.DefaultInterceptionAsserter.AssertThatMethodCanBeInterceptedOnInstance(Metho dInfo method, Object callTarget)
at FakeItEasy.Configuration.FakeConfigurationManager.CallTo(Expression`1 callSpecification)
at ProdMaster.Hosts.Web.Tests.Unit.Controllers.AccountControllerTests.SetUp() in AccountControllerTests.cs: line 44

所以问题实际上是如何使用 FakeItEasy 伪造静态方法。

TIA,

大卫

最佳答案

在 FakeItEasy 中无法拦截静态方法(目前还没有其他开源、免费的 .Net 模拟框架)。为了能够模拟静态(和密封类),您必须从 Telerik 购买 Typemock Isolator 或 Just Mock。

许多开发人员认为静态是代码异味(在大多数情况下)。因此,开源模拟框架不支持这一点被视为一件好事,因为它促进了更好的设计。模拟的“黄金法则”是“如果你不能控制它,就不要模拟它”,所以解决你遇到的问题的常用方法是围绕静态调用创建一个包装器。您测试与这个 - 可模拟 - 包装器的交互。 System.DateTime.Now 是您经常希望在测试中测试的静态示例。要将您的测试与此隔离,您可以执行以下操作:

public interface ISystemTimeProvider
{
DateTime Now { get; }
}

public class DateTimeNowSystemTimeProvider
: ISystemTimeProvider
{
public DateTime Now
{
get
{
return DateTime.Now;
}
}
}

使用上述接口(interface)和实现,您的 SUT 将依赖于接口(interface)(例如通过构造函数注入(inject))。在你的测试中,你会用一个假的( A.Fake<ISystemTimeProvider>() )注入(inject)它。 DateTimeSystemTimeProvider 的实现永远不会进行单元测试,它的级别非常低,除了集成测试之外不需要任何测试。

我对你所说的验证码库不是很熟悉,所以我不确定在这种情况下你将如何应用上述模式,但我确信它可以通过一种或另一种方式完成。

关于unit-testing - 来自 commonlibnet 的 FakeItEasy 和 FluentValidation 的假验证码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5742425/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com