.

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
        }
    }
}

 

nunit template for VS2010

here is the template for nunit (from: )

download: TestClassLibrary or a test example project

copy to : C:\Users\pete\Documents\Visual Studio 2010\My Exported Templates

all i did was export it from file-export template – and this is what was rendered

it’s in visual c# > called: testclasslibrary

 

simplified:

[TestFixture]
public class Class1
{
    [Test]
    public void test1()
    {
        Assert.True(true);
    }
}

Unit test protected methods using Rhino mocks

If your method is in a different assembly then you need to allow access to your own assembly (e.g MyAssembly) and e.g. Rhino mocks () or whatever you use for mocking

you can do so:

[assembly: InternalsVisibleTo("MyAssembly")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

then your unit test would look something like this:

[TestMethod]
public void TestMethod1()
{
    var txt = "testdata";
    var rawRequestProcessor = MockRepository.GenerateStub<RawRequestProcessor>();
    rawRequestProcessor.Process(txt);
    Assert.AreEqual(txt, rawRequestProcessor.RawInput);
    rawRequestProcessor.Expect(m => m.processInput());
    rawRequestProcessor.VerifyAllExpectations();
}

example of the code being tested here:

public class RawRequestProcessor
{
    public void Process(string txt)
    {
        RawInput = txt;
    }

    private string _rawInput;
    public string RawInput {
        get { return _rawInput; }
        set
        {
            _rawInput = value;
            processInput();
        }
    }

    internal virtual void processInput()
    {
            
    }
}