1
votes

Environment
Rhino Mocks version: 3.6.1
Visual Studio: 2017
Resharper: 2017.3.3
Nunit: 2.6.4

I run the 2 test fixtures together using Resharper. When I do so I get an error:

System.InvalidOperationException : Previous method '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, Function2 action) at Rhino.Mocks.RhinoMocksExtensions.Stub[T,R](T mock, Function2 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)

When I run the two test fixtures multiple times, the test source for the error changes constantly between TestFixture1.Test1 to TestFixture2.Test1. When I run the test fixtures in isolation everything passes. Is this an issue with Rhino Mocks and threading?

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();
    }
}
1
does the same happen if you use Expect vs Stub? Reference Rhino Mocks - Stub .Expect vs .AssertWasCalledNkosi

1 Answers

0
votes

Yes. Your code proves that Rhino Mocks is not thread safe. I'd advise you to use Moq since it should work well in parallel.