Search code examples
c#unit-testingnunitnmock

NMOCK3 - Expects.One.Method


I am new to Mocking and Unit Testing in general. Please see the code below which I found online:

[Test]
        public void CanQueryViewUseAccountServiceToFundsTransfer()
        {
            _viewMock.Expects.One.Method(v => v.GetSourceAccount()).WillReturn("1234");
            _viewMock.Expects.One.GetProperty(v => v.TargetAccount).WillReturn("9876");
            _viewMock.Expects.One.GetProperty(v => v.TransferAmount).WillReturn(200.00m);
            _serviceMock.Expects.Exactly(1).Method(s => s.TransferFunds(null, null, 0m)).With("1234", "9876", 200.00m);
            _presenter.Transfer_Clicked();
            _mocks.VerifyAllExpectationsHaveBeenMet();
        }

I cannot find any documentation at all that explain what the following lines do:

_viewMock.Expects.One.Method //Is this saying it is expecting one and only one function to be called?
_viewMock.Expects.One.GetProperty

I have tried to find answers myself. For example I looked here: http://nmock3.codeplex.com/documentation, but all I can find is FAQs.


Solution

  • Basing on the source code of ExpectsOfT.cs:

    public IMethodSyntax<T> One
    {
        get
        {
            return Exactly(1);
        }
    }
    

    One might draw a conculsion that .One refers to exactly one expectation/call to the given method.

    I am new to Mocking and Unit Testing in general.

    Any reason you are learning with NMock? As far as I can see it's not very popular and doesn't seem to be actively developed. There are libraries with far better documentation and community, for example Moq (sample of more or less equivalent code):

    _viewMock.Setup(v => v.GetSourceAccount()).Returns("1234");
    

    or FakeItEasy

    A.CallTo(() => _viewMock.GetSourceAccount()).Returns("1234");