【发布时间】:2018-10-16 16:22:53
【问题描述】:
环境
Rhino Mocks 版本:3.6.1
视觉工作室:2017
Resharper:2017.3.3
单元:2.6.4
我使用 Resharper 一起运行 2 个测试装置。当我这样做时,我得到一个错误:
System.InvalidOperationException:以前的方法“IService.DoThing();”需要返回值或抛出异常。 在 Rhino.Mocks.Impl.RecordMockState.AssertPreviousMethodIsClose() 在 Rhino.Mocks.Impl.RecordMockState.Replay() 在 Rhino.Mocks.MockRepository.ReplayCore(Object obj, Boolean checkInsideOrdering) 在 Rhino.Mocks.RhinoMocksExtensions.Expect[T,R](T mock, Function
2 action) at Rhino.Mocks.RhinoMocksExtensions.Stub[T,R](T mock, Function2 action) 在 C:\SCM\MyProject.Tests.Unit\ExampleTests.cs:line 42 中的 MobileServices.Api.Tests.Unit.TestFixture2.d__2.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 NUnit.Framework.Internal.AsyncInvocationRegion.AsyncTaskInvocationRegion.WaitForPendingOperationsToComplete(对象调用结果) 在 NUnit.Framework.Internal.Commands.TestMethodCommand.RunAsyncTestMethod(TestExecutionContext context)
当我多次运行这两个测试夹具时,错误的测试源在 TestFixture1.Test1 到 TestFixture2.Test1 之间不断变化。当我单独运行测试装置时,一切都通过了。这是 Rhino Mocks 和线程的问题吗?
using Project.Tests.Unit.SupportingClasses;
using NUnit.Framework;
using System.Threading.Tasks;
using Rhino.Mocks;
namespace Project.Tests.Unit
{
[TestFixture]
public class TestFixture1
{
private IService _service;
[SetUp]
public void SetUp()
{
_service = MockRepository.GenerateMock<IService>();
}
[Test]
public async Task Test1()
{
_service.Stub(s => s.DoThing()).Return(Task.FromResult(true));
var response = await _service.DoThing();
}
}
[TestFixture]
public class TestFixture2
{
private IService _service;
[SetUp]
public void SetUp()
{
_service = MockRepository.GenerateMock<IService>();
}
[Test]
public async Task Test1()
{
_service.Stub(s => s.DoThing()).Return(Task.FromResult(true));
var response = await _service.DoThing();
}
}
}
namespace Project.Tests.Unit.SupportingClasses
{
public interface IService
{
Task<bool> DoThing();
}
}
【问题讨论】:
-
如果您使用
Expect与Stub,会发生同样的情况吗?参考Rhino Mocks - Stub .Expect vs .AssertWasCalled
标签: c# unit-testing asynchronous async-await rhino-mocks