奇怪的“预期对模拟至少调用一次,但从未执行过”我设置模拟时出错

发布于 2024-12-11 22:33:12 字数 2346 浏览 0 评论 0原文

我通过 NUnit 从 Moq 收到此错误,这对我来说没有多大意义。

“预期在模拟上至少调用一次,但从未执行过:x => x.DeleteItem(.$VB$Local_item)”

“在 Moq.Mock.ThrowVerifyException(MethodCall Expects, IEnumerable1 setups, IEnumerable< /code>1actualCalls,Expression表达式,Times次,Int32 callCount) 在 Moq.Mock.VerifyCalls(拦截器 targetInterceptor,预期 MethodCall,表达式表达式,Times 次) 在 Moq.Mock.Verify[T](Mock 模拟,Expression1 表达式,Times 次,String failureMessage) 在 Moq.Mock1.Verify(Expression`1 表达式) 在 PeekABookEditor.UnitTests.ItemBrowsing.Can_Delete_Item() in C:\Projects\MyProject\MyProject.UnitTests\Tests\ItemBrowsing.vb:line 167"

非常相似的代码在 C# 中运行良好,因此对我而言,错误可能很小且语法错误这

是我的代码:

    <Test()> _
Public Sub Can_Delete_Item()
    'Arrange: Given a repository containing some item...
    Dim mockRepository = New Mock(Of IItemsRepository)()
    Dim item As New Item With {.ItemID = "24", .Title = "i24"}

    mockRepository.Setup(Function(x) x.Items).Returns(New Item() {item}.AsQueryable())

    'Act ... when the user tries to delete that product
    Dim controller = New ItemsController(mockRepository.Object)
    Dim result = controller.Delete(24)

    'Assert ... then it's deleted, and the user sees a confirmation
    mockRepository.Verify(Sub(x) x.DeleteItem(item))
    result.ShouldBeRedirectionTo(New With {Key .action = "List"})
    Assert.AreEqual(DirectCast(controller.TempData("message"), String), "i24 was deleted")

End Sub

有罪的行似乎是“mockRepository.Verify(Sub(x)” x.DeleteItem(item))"

关于如何解决此问题有什么想法吗?

工作 C# 代码并不完全相同,但它是:

[Test] 
public void Can_Delete_Product() 
{ 
// Arrange: Given a repository containing some product... 
var mockRepository = new Mock<IProductsRepository>(); 
var product = new Product { ProductID = 24, Name = "P24"}; 
mockRepository.Setup(x => x.Products).Returns( 
    new[] { product }.AsQueryable() 
); 

// Act: ... when the user tries to delete that product 
var controller = new AdminController(mockRepository.Object); 
var result = controller.Delete(24); 

// Assert: ... then it's deleted, and the user sees a confirmation
mockRepository.Verify(x => x.DeleteProduct(product)); 
result.ShouldBeRedirectionTo(new { action = "Index" }); 
controller.TempData["message"].ShouldEqual("P24 was deleted"); 

}

I'm getting this error from Moq via NUnit, and it doesn't make much in the way of sense to me.

"Expected invocation on the mock at least once, but was never performed: x => x.DeleteItem(.$VB$Local_item)"

"at Moq.Mock.ThrowVerifyException(MethodCall expected, IEnumerable1 setups, IEnumerable1 actualCalls, Expression expression, Times times, Int32 callCount)
at Moq.Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times)
at Moq.Mock.Verify[T](Mock mock, Expression1 expression, Times times, String failMessage)
at Moq.Mock
1.Verify(Expression`1 expression)
at PeekABookEditor.UnitTests.ItemBrowsing.Can_Delete_Item() in C:\Projects\MyProject\MyProject.UnitTests\Tests\ItemBrowsing.vb:line 167"

Very similar code works well in C#, so the error might be minor and syntactical on my part.

Here's my code:

    <Test()> _
Public Sub Can_Delete_Item()
    'Arrange: Given a repository containing some item...
    Dim mockRepository = New Mock(Of IItemsRepository)()
    Dim item As New Item With {.ItemID = "24", .Title = "i24"}

    mockRepository.Setup(Function(x) x.Items).Returns(New Item() {item}.AsQueryable())

    'Act ... when the user tries to delete that product
    Dim controller = New ItemsController(mockRepository.Object)
    Dim result = controller.Delete(24)

    'Assert ... then it's deleted, and the user sees a confirmation
    mockRepository.Verify(Sub(x) x.DeleteItem(item))
    result.ShouldBeRedirectionTo(New With {Key .action = "List"})
    Assert.AreEqual(DirectCast(controller.TempData("message"), String), "i24 was deleted")

End Sub

The guilty line appears to be "mockRepository.Verify(Sub(x) x.DeleteItem(item))"

Any thoughts on how to fix this?

Working C# code isn't the exact same, but here it is:

[Test] 
public void Can_Delete_Product() 
{ 
// Arrange: Given a repository containing some product... 
var mockRepository = new Mock<IProductsRepository>(); 
var product = new Product { ProductID = 24, Name = "P24"}; 
mockRepository.Setup(x => x.Products).Returns( 
    new[] { product }.AsQueryable() 
); 

// Act: ... when the user tries to delete that product 
var controller = new AdminController(mockRepository.Object); 
var result = controller.Delete(24); 

// Assert: ... then it's deleted, and the user sees a confirmation
mockRepository.Verify(x => x.DeleteProduct(product)); 
result.ShouldBeRedirectionTo(new { action = "Index" }); 
controller.TempData["message"].ShouldEqual("P24 was deleted"); 

}

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

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

发布评论

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

评论(1

独闯女儿国 2024-12-18 22:33:12

在 VB 测试方法中,您使用字符串 ItemID = "24" 创建 Item,但使用整数调用 controller.Delete 方法值为24

检查您的控制器代码并查看类型差异是否会导致项目无法正确识别,因此要么根本不调用 DeleteItem,要么使用不同的 Item 调用。

In your VB test method, you create Item with a string ItemID = "24", but you call the controller.Delete method with an integer value of 24.

Check your controller code and see if the type discrepancy results in the item not being identified correctly, so either DeleteItem is not called at all, or is called with a different Item.

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