.

Stubbing a method with multiple return values

This is how you can stub a method to return different values on each call.

TestPublishNewStandsLogResultIndex is the indexer that indicates which result is the next in line.

WhenCalled is the rhinoMocks way of accessing this predefined list.

 

using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FloorPlanning.Framework.Logging;
using FloorPlanning.Service.Controllers.ApiControllers.v1;
using Rhino.Mocks;

namespace Service.Tests.Handlers
{
    [TestClass]
    public class MyControllerTest
    {
        private static int TestPublishNewStandsLogResultIndex;

        private static readonly List<LogEntry> TestPublishNewStandsLogResults = new List<LogEntry>
        {
            new LogEntry
            {
                Id = 32,
            },
            new LogEntry
            {
                Id = 55,
            },
            null,
            };

        private IOperationLogHandler operationLogHandler;

        [TestInitialize]
        public void Setup()
        {
              operationLogHandler = MockRepository.GenerateMock<IOperationLogHandler>();
        }

        [TestMethod]
        public void PublishNewStandsSuccessful()
        {
            operationLogHandler.Stub(m => m.GetLatestLogEntryForOperation(Operation.PublishStandUpdates))
                .WhenCalled(mi => mi.ReturnValue = TestPublishNewStandsLogResults[TestPublishNewStandsLogResultIndex++]);

            var controller = new PublishingController(operationLogHandler);

            var result = controller.Stands();

            //...asserts here
        }
    }
}

 

What's your thoughts on this?

*

Protected by WP Anti Spam