已使用包含“x”的 IEnumerable 调用验证方法;具有最小起订量的元素

发布于 2024-11-13 07:30:45 字数 909 浏览 4 评论 0原文

我有一个带有 Add 方法的存储库,该方法采用 IEnumerable 作为参数:

public void Add<T>(T item) where T : class, new(){}

在单元测试中,我想验证是否使用 IEnumerable 调用此方法,该 IEnumerable 包含与另一个 IEnumerable 完全相同数量的元素

[Test]
public void InvoicesAreGeneratedForAllStudents()
{
    var students = StudentStub.GetStudents();
    session.Setup(x => x.All<Student>()).Returns(students.AsQueryable());

    service.GenerateInvoices(Payments.Jaar, DateTime.Now); 

    session.Verify(x => x.Add(It.Is<IEnumerable<Invoice>>(
        invoices => invoices.Count() == students.Count())));
 }

单元测试的结果:

Moq.MockException : 
Expected invocation on the mock at least once, but was never performed: 

x => x.Add<Invoice>(It.Is<IEnumerable`1>(i => i.Count<Invoice>() == 10))

No setups configured.

我是什么做错了吗?

I have a repository with an Add method that takes an IEnumerable as parameter:

public void Add<T>(T item) where T : class, new(){}

In a unittest I want to verify that this method is called with an IEnumerable that contains exactly the same amount of elements as another IEnumerable

[Test]
public void InvoicesAreGeneratedForAllStudents()
{
    var students = StudentStub.GetStudents();
    session.Setup(x => x.All<Student>()).Returns(students.AsQueryable());

    service.GenerateInvoices(Payments.Jaar, DateTime.Now); 

    session.Verify(x => x.Add(It.Is<IEnumerable<Invoice>>(
        invoices => invoices.Count() == students.Count())));
 }

Result of the unit test:

Moq.MockException : 
Expected invocation on the mock at least once, but was never performed: 

x => x.Add<Invoice>(It.Is<IEnumerable`1>(i => i.Count<Invoice>() == 10))

No setups configured.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月打碎记忆 2024-11-20 07:30:45

从您的代码示例中,您尚未设置 x =>;起订量上的 x.Add

session.Setup(x => x.Add(It.IsAny<IEnumerable>());

除非 x.All 的设置是 x.Add?如果是这样,您需要精确匹配验证和设置 - 一个好的方法是将其提取到返回表达式的通用方法。

编辑:添加了一个示例,我更改了 Add 的签名,因为我看不到如何传递集合。

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        Mock<Boo> moqBoo = new Mock<Boo>();
        moqBoo.Setup(IEnumerableHasExpectedNumberOfElements(10));

        // ACT

        moqBoo.Verify(IEnumerableHasExpectedNumberOfElements(10));
    }

    private static Expression<Action<Boo>> IEnumerableHasExpectedNumberOfElements(int expectedNumberOfElements)
    {
        return b => b.Add(It.Is<IEnumerable<Invoice>>(ie => ie.Count() == expectedNumberOfElements));
    }
}

public class Boo
{
    public void Add<T>(IEnumerable<T> item) where T : class, new()
    {
    }
}

public class Invoice
{

}

另外,调试这些东西的一个好方法是使用 MockBehavior.Strict 设置 Mock,然后调用的代码会通知您需要配置什么。

From your code example you haven't set up the x => x.Add on the Moq

session.Setup(x => x.Add(It.IsAny<IEnumerable>());

Unless the Setup for x.All is meant to be x.Add? If so, you need to match the Verify and Setup exactly - a good way to do that is to extract it to a common method that returns an Expression.

EDIT: Added a sample, I have changed the signature of Add as I can't see how you could pass a collection otherwise.

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        Mock<Boo> moqBoo = new Mock<Boo>();
        moqBoo.Setup(IEnumerableHasExpectedNumberOfElements(10));

        // ACT

        moqBoo.Verify(IEnumerableHasExpectedNumberOfElements(10));
    }

    private static Expression<Action<Boo>> IEnumerableHasExpectedNumberOfElements(int expectedNumberOfElements)
    {
        return b => b.Add(It.Is<IEnumerable<Invoice>>(ie => ie.Count() == expectedNumberOfElements));
    }
}

public class Boo
{
    public void Add<T>(IEnumerable<T> item) where T : class, new()
    {
    }
}

public class Invoice
{

}

Also, a good way to debug these things is to set your Mock up with MockBehavior.Strict and then you'll be informed by the invoked code what you need to configure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文