gpt4 book ai didi

c# - 为什么 MSpec 中对 FakeItEasy 对象的 MustHaveHappened 调用失败?

转载 作者:行者123 更新时间:2023-11-30 22:19:42 25 4
gpt4 key购买 nike

当运行以下测试时,所有断言都失败了。无法弄清楚为什么它们会失败,因为实际的实现确实有对对象的调用。

这是一个已知错误吗?因为其他一些测试确实成功了。

[Subject("Pst Cleanup")]
public class When_running_Pst_CleanUp
{
Establish context = () =>
{
_folders = A.Fake<IOutlookFolderParameters>();
_processor = A.Fake<IOutlookPstCleaner>();
};

Because of = () => _processor.Cleanup(_folders);

It should_cleanup_the_mail_folder = () => A.CallTo(() => _folders.EmailFolder).MustHaveHappened();
It should_cleanup_tasks_folder = () => A.CallTo(() => _folders.TaskFolder).MustHaveHappened();
It should_cleanup_appointments_folder = () => A.CallTo(() => _folders.AppointmentFolder).MustHaveHappened();

private static IOutlookPstCleaner _processor;
private static IOutlookFolderParameters _folders;
}

Assertion failed for the following call: Outlook.Contracts.IOutlookFolderParameters.get_NotificationsFolder() Expected to find it at least once but no calls were made to the fake object.

at FakeItEasy.Core.FakeAsserter.AssertWasCalled(Func2 callPredicate, String callDescription, Func2 repeatPredicate, String repeatDescription) at FakeItEasy.Configuration.RuleBuilder.MustHaveHappened(Repeated repeatConstraint) at UnitTests.When_running_Pst_CleanUp.<.ctor>b__2() in When_running_Pst_CleanUp.cs: line 19

最佳答案

这是 FakeItEasy 绝对正确的行为。您需要使用 IOutlookPstCleaner 的实际实现才能使您的测试成功。始终确保你伪造了正确的东西,不要意外伪造你的 SUT。

通过测试刚被调用的属性,您绝对不会测试任何有值(value)的东西。我也可以只为 IOutlookPstCleaner 编写这个实现,这样您的测试就会成功:

public class Cleaner : IOutlookPstCleaner 
{
public void Cleanup(IOutlookFolderParameters folders)
{
var email = folders.EmailFolder;
var task = folders.TaskFolder;
var appointment = folders.AppointmentFolder;
}
}

如果您发布了IOutlookPstCleaner 的实现,我很乐意帮助您找到正确的测试对象。<​​/p>

关于c# - 为什么 MSpec 中对 FakeItEasy 对象的 MustHaveHappened 调用失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15431542/

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