简体   繁体   English

Rhino Mocks异步单元测试

[英]Rhino Mocks async unit testing

Environment 环境
Rhino Mocks version: 3.6.1 犀牛版:3.6.1
Visual Studio: 2017 Visual Studio:2017年
Resharper: 2017.3.3 收割者:2017.3.3
Nunit: 2.6.4 单位:2.6.4

I run the 2 test fixtures together using Resharper. 我使用Resharper一起运行2个测试夹具。 When I do so I get an error: 当我这样做时,我得到一个错误:

System.InvalidOperationException : Previous method 'IService.DoThing();' System.InvalidOperationException:先前的方法'IService.DoThing();' requires a return value or an exception to throw. 需要返回值或引发异常。 at Rhino.Mocks.Impl.RecordMockState.AssertPreviousMethodIsClose() at Rhino.Mocks.Impl.RecordMockState.Replay() at Rhino.Mocks.MockRepository.ReplayCore(Object obj, Boolean checkInsideOrdering) at Rhino.Mocks.RhinoMocksExtensions.Expect[T,R](T mock, Function 2 action) at Rhino.Mocks.RhinoMocksExtensions.Stub[T,R](T mock, Function 2 action) at MobileServices.Api.Tests.Unit.TestFixture2.d__2.MoveNext() in C:\\SCM\\MyProject.Tests.Unit\\ExampleTests.cs:line 42 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at NUnit.Framework.Internal.AsyncInvocationRegion.AsyncTaskInvocationRegion.WaitForPendingOperationsToComplete(Object invocationResult) at NUnit.Framework.Internal.Commands.TestMethodCommand.RunAsyncTestMethod(TestExecutionContext context) 在Rhino.Mocks.Impl.RecordMockState.AssertPreviousMethodIsClose()在Rhino.Mocks.Impl.RecordMockState.Replay()在Rhino.Mocks.MockRepository.ReplayCore(Object obj,Boolean checkInsideOrdering)在Rhino.Mocks.RhinoMocksExtensions.Expect [T,在C的MobileServices.Api.Tests.Unit.TestFixture2.d__2.MoveNext() 2 action) at Rhino.Mocks.RhinoMocksExtensions.Stub[T,R](T mock, Function 2动作)在S: \\ SCM \\ MyProject.Tests.Unit \\ ExampleTests.cs:第42行---从上一个引发异常的位置开始的堆栈跟踪---位于NUnit.Framework.Internal的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() NUnit.Framework.Internal.Commands.TestMethodCommand.RunAsyncTestMethod(TestExecutionContext上下文)处的.AsyncInvocationRegion.AsyncTaskInvocationRegion.WaitForPendingOperationsToComplete(Object invocationResult)

When I run the two test fixtures multiple times, the test source for the error changes constantly between TestFixture1.Test1 to TestFixture2.Test1. 当我多次运行两个测试夹具时,错误的测试源在TestFixture1.Test1到TestFixture2.Test1之间不断变化。 When I run the test fixtures in isolation everything passes. 当我独立运行测试装置时,一切都通过了。 Is this an issue with Rhino Mocks and threading? 这是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();
    }
}

Yes. 是。 Your code proves that Rhino Mocks is not thread safe . 您的代码证明Rhino Mocks 不是线程安全的 I'd advise you to use Moq since it should work well in parallel. 我建议您使用Moq因为它可以并行运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM