Why does this not work as expected?

3 posts, 0 answers
  1. Ronald
    Ronald avatar
    12 posts
    Member since:
    Aug 2010

    Posted 03 Nov 2010 Link to this post

    Please see the narrowed down code-example below.

    What I expect is the method IsElf() to return a value of TRUE.
    When I debug the UnitTest, mensMock.Leeftijd returns a value of 11. When I look at the value of actual, it's value = false???
    It seems that the Mock object is not 'routing' the call as expected...

    When I use the Behaviour.CallOriginal setting (which is not documented BTW) when Creating the Mock everything works as expected. Loose and Strict mode both fail.

    I really like your concept of mocking, could you please tell me what I'm doing wrong?

    Kind regards from Holland & TIA,

    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Telerik.JustMock;
    using PatrickMocks;
    namespace PatrickMocks
        public class Mens
            public int Leeftijd { getset; }
            public virtual bool IsElf()
    //            this.Foo();
                return (this.Leeftijd == 11);
            public virtual void Foo()
    namespace MockTest
        public class MensTest
            public void IsElfTest()
                // Arrange
                Mens mensMock = Mock.Create<PatrickMocks.Mens>();
                //Mens mensMock = Mock.Create<PatrickMocks.Mens>(Behavior.CallOriginal);             //<== THIS WORKS OK!!!
                Mock.Arrange(() => mensMock.Foo()).CallOriginal().MustBeCalled();

                // Act
                bool expected = true// TODO: Initialize to an appropriate value
                bool actual = true;
                mensMock.Leeftijd = 11;
                actual = mensMock.IsElf();

                // Assert
                Assert.AreEqual(expected, actual);
                Mock.Assert(mensMock);       // Check dat deze method gedraaid heeft!
  2. Ronald
    Ronald avatar
    12 posts
    Member since:
    Aug 2010

    Posted 03 Nov 2010 Link to this post

    I think I understand it a bit better now. Using the CallOriginal Behaviour (which, as opposed to what I said, is documented in the CHM, but I could not find it on the Web Documentation?) the call will be passed to the orginal by default.

    But I think the documentation is confusing...
    Under 'Stict Mocking' is says:
    In a loose mock, any call to a method that is not arranged, results in a call to the original method implementation.
    That is not what I am experiencing...

    Under the coumentation in the Behaviour Enumerator is stated:
    Loose --> Specifies the mock as loose. This means by default it will behave like a stub or return default value

    AFAI understand this,I get a default value when I call a method that is not arranged and that is what I'm seeing when I call IsElf in the example in my last post. I get a value of 'False'.

    CallOriginal --> Specifies that by default all calls made on mock will invoke its corresponding original member unless some expecations is set.
    When I set this behaviour everything works as expected.

    I have a few questions left:
    - Are my conclusion above right or am I missing a point (as usual<g>) here.
    - Why is CallOriginal not the Default Behaviour...

    Again, TIA for your time...

  3. DevCraft R3 2016 release webinar banner
  4. Ricky
    Ricky avatar
    467 posts

    Posted 03 Nov 2010 Link to this post

    Hi Ronald,

    Thank you for bringing up the question. Now, when you create a mocked instance by definition it will be not be calling any original members of that type or in other words it will behave like a stub (loose mocking) that is the reason it is a mocked object and not a real one. In cases when you want certain calls to be mocked or executed according to your expected behavior and the rest should invoke its original implementation, during Mock.Create<> you must provide Behavior.CallOriginal switch. 

    Now, in your test when Behavior.CallOriginal is not specified during the creation of PatrickMocks.Mens mock, as you call IsElf() it invokes the fake implementation and thus the call never goes to the following line:

    return (this.Leeftijd == 11);

    Therefore the test fails.  In that case the test should be written in the following way if Behavior.CallOriginal is not specified during Mock.Create<>:

    // Arrange            
    Mens mensMock = Mock.Create<PatrickMocks.Mens>();            
    Mock.Arrange(() => mensMock.Foo()).CallOriginal().MustBeCalled();
    Mock.Arrange(() => mensMock.IsElf()).CallOriginal();            
    bool expected = true;
    bool actual = true;           
    mensMock.Leeftijd = 11;            
    actual = mensMock.IsElf();             
    Assert.AreEqual(expected, actual);            

    Finally, this is actually a default behavior to all the mocking tools which is by default it will act as a stub unless you specify further.

    Hope that the information is useful.

    Kind Regards,
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top