Stub or Expect, AssertWasCalled or VerifyAllExceptions

Regarding the following unit-test principles, I think Stub + AsserWasCalled is better.

  • Each test should only test one thing,
  • All setup code should be in one place,
  • Test behavior not method.

using System.Collections.Generic;
using NUnit.Framework;
using Rhino.Mocks;

namespace Demo
{
    public interface IDependencyA {IEnumerable<string> GetList();}

    public interface IDependencyB {string DoSth(IEnumerable<string> input);}

    public interface IDependencyC {string DoSthElse(string input);}

    public class SystemToTest
    {
        private readonly IDependencyA _a;
        private readonly IDependencyB _b;
        private readonly IDependencyC _c;

        public SystemToTest(IDependencyA a, IDependencyB b, IDependencyC c)
        {
            _a = a;
            _b = b;
            _c = c;
        }

        public string Action()
        {
            return _c.DoSthElse(_b.DoSth(_a.GetList()));
        }
    }

    [TestFixture]
    public class MockDemo
    {
        protected IDependencyA _mockDependencyA;
        protected IDependencyB _mockDependencyB;
        protected IDependencyC _mockDependencyC;

        private SystemToTest _systemUnderTest;

        [SetUp]
        public virtual void Setup()
        {
            _mockDependencyA = MockRepository.GenerateMock<IDependencyA>();
            _mockDependencyB = MockRepository.GenerateMock<IDependencyB>();
            _mockDependencyC = MockRepository.GenerateMock<IDependencyC>();

            _systemUnderTest = new SystemToTest(_mockDependencyA,
                                                _mockDependencyB,
                                                _mockDependencyC);

            // If B.DoSth can NOT run with null input, this stub is needed.
            _mockDependencyA.Stub(x => x.GetList()).Return(
                new List<string>()
                );
        }

        [TestFixture]
        public class When_Action_happens : MockDemo
        {
            [SetUp]
            public override void Setup()
            {
                base.Setup();
                _systemUnderTest.Action();
            }

            [Test]
            public void should_ask_a_to_get_list()
            {
                 _mockDependencyA.AssertWasCalled(x => x.GetList());
                // Can also run _mockRequestRepository.VerifyAllExpectations();
                //  if using Expect in Setup instead of Stub
            }

            [Test]
            public void should_ask_b_to_do_sth()
            {

                _mockDependencyB.AssertWasCalled(
                    x => x.DoSth(null),
                    opt => opt.IgnoreArguments());
            }

            [Test]
            public void should_ask_c_to_do_sth_else()
            {
                _mockDependencyC.AssertWasCalled(
                    x => x.DoSthElse(null),
                    opt => opt.IgnoreArguments());
            }
        }
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s