This is a migrated thread and some comments may be shown as answers.

Why does this not work as expected?

2 Answers 83 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Ronald
Top achievements
Rank 1
Ronald asked on 03 Nov 2010, 12:24 PM
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,

Ronald.
----
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()
        {
            this.Leeftijd++;
            this.Leeftijd--;
        }
    }
}
namespace MockTest
{
    [TestClass()]
    public class MensTest
    {
        [TestMethod()]
        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 Answers, 1 is accepted

Sort by
0
Ronald
Top achievements
Rank 1
answered on 03 Nov 2010, 12:56 PM
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:
Note
Note
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...

Ronald.
0
Ricky
Telerik team
answered on 03 Nov 2010, 01:52 PM
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:

this.Foo();            
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);            
Mock.Assert(mensMock);       

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
Tags
General Discussions
Asked by
Ronald
Top achievements
Rank 1
Answers by
Ronald
Top achievements
Rank 1
Ricky
Telerik team
Share this question
or