C# 为什么会出现此错误:需要抛出返回值或异常?

C# 为什么会出现此错误:需要抛出返回值或异常?,c#,unit-testing,tdd,rhino-mocks,C#,Unit Testing,Tdd,Rhino Mocks,当我尝试运行测试时,出现以下错误。我试过忽略参数(),但这似乎不起作用。只有当我尝试模拟.Ordered()时才会引发此异常。如果我尝试模拟.Record(),则它会通过 System.InvalidOperationException:以前的方法 'IPProductRepository.GetAllProducts();'需要返回值或 要抛出的异常 不是100%确定,但将其作为答案删除,以便您可以轻松阅读答案/评论如果这不是解决方案,请告诉我,然后我将删除答案以避免混淆。 我想你的问题出在

当我尝试运行测试时,出现以下错误。我试过
忽略参数()
,但这似乎不起作用。只有当我尝试模拟.Ordered()时才会引发此异常。如果我尝试模拟.Record(),则它会通过

System.InvalidOperationException:以前的方法 'IPProductRepository.GetAllProducts();'需要返回值或 要抛出的异常



不是100%确定,但将其作为答案删除,以便您可以轻松阅读答案/评论如果这不是解决方案,请告诉我,然后我将删除答案以避免混淆。

我想你的问题出在

Expect.Call(()=>productRepository.SaveProduct(产品))

您已经处于录制模式(
mock.Ordered()
),因此无需指定
Expect.Call()

Expect.Call()
要求返回值。您只是在寻找一种方法来验证是否正在调用save方法

我认为您应该将代码更改为:

        using (mock.Ordered())
        {
            Expect.Call(productRepository
                .GetAllProducts())
                .IgnoreArguments()
                .Return(new ArrayList());
            productRepository.SaveProduct(product);
        }
(您可能认为您在测试方法中调用了SaveProduct,但您不会,RhinoMocks比这更聪明,并且会记录这个无效调用)

这与您描述的错误完全相同,但失败了。 System.InvalidOperationException:以前的方法“IPProductRepository.GetAllProducts();”需要返回值或异常才能引发

原因是模拟存储库从未离开记录模式。您可能认为(我也这么认为)
mock.Ordered()
会将存储库置于某种有序记录状态,但事实并非如此。因此,您需要指定录制行为的时间

您可以通过两种方式执行此操作:

        using (mock.Record())
        using (mock.Ordered())
        {
            Expect.Call(productRepository.GetAllProducts()).Return(new ArrayList());
            Expect.Call(() => productRepository.SaveProduct(product));
        }
或在完成录制后删除mock.ReplayAll():

        using (mock.Ordered())
        {
            Expect.Call(productRepository.GetAllProducts()).Return(new ArrayList());
            Expect.Call(() => productRepository.SaveProduct(product));
        }

        mock.ReplayAll();

        using (mock.Playback())
        {
            service.GetAllProducts();
            service.SaveProduct(product);
        }

我测试了两个,都有效。

那么您的服务电话是什么样子的?这真的是一个很薄的层,它只是将请求逐字地传递到您的存储库中吗?这不是关于您可以拥有什么,而是关于您的代码拥有什么?@JonSkeet,我将其放在上面的编辑中。现在您显示了一个服务调用,但您在测试中进行了两个…@JonSkeet,现在添加了另一个服务呼叫。我尝试了上面的建议,我仍然遇到相同的错误。请保留此答案,因为它提供了有关工作或订单的信息,请期待。k稍等,然后我将尝试重现您的错误。需要启动VS。谢谢,当你得到更多细节时,请告诉我。
        using (mock.Ordered())
        {
            Expect.Call(productRepository
                .GetAllProducts())
                .IgnoreArguments()
                .Return(new ArrayList());
            Expect.Call(() => productRepository.SaveProduct(product));
        }
        using (mock.Record())
        using (mock.Ordered())
        {
            Expect.Call(productRepository.GetAllProducts()).Return(new ArrayList());
            Expect.Call(() => productRepository.SaveProduct(product));
        }
        using (mock.Ordered())
        {
            Expect.Call(productRepository.GetAllProducts()).Return(new ArrayList());
            Expect.Call(() => productRepository.SaveProduct(product));
        }

        mock.ReplayAll();

        using (mock.Playback())
        {
            service.GetAllProducts();
            service.SaveProduct(product);
        }