我使用commonlibrary(http://commonlibrarynet.codeplex.com/)的验证码类。我的代码工作和一切,但现在我试图编写单元测试。FakeItEasy和FluentValidation从commonlibnet伪造的验证码
我的验证规则是:
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,
大卫
谢谢你在这里和通过电子邮件的答案。非常感谢。我并没有试图嘲笑Captcha控制,而是扼杀了它。从Roy Osherove关于单元测试的书中我已经阅读和理解的内容可以看出,这是我应该做的,因为它是一种外部依赖性,不应该使我的测试失败。无论如何,我相信我已经理解了你的观点,并且会尝试调整提供给Captcha控件的代码。再次感谢你。大卫 – DavidS 2011-04-24 12:10:34