How to Arrange a base class method?

6 posts, 1 answers
  1. Md.Hasanuzzaman
    Md.Hasanuzzaman avatar
    31 posts
    Member since:
    Apr 2011

    Posted 25 Feb 2014 Link to this post

    I am trying Arrange a  base class method which is override in drive class but I can`t

    Here is my Code :

     public class Class1 : Class2
        {
            public bool Class1Call { get; set; }

            public override void Method1()
            {
                Class1Call = true;
               base.Method1();      //<<<<< want to ensure that base.Method1 ic call during Method1 call
            }
        }

        public class Class2
        {
            public bool IsCall { get; set; }

            public virtual void Method1()
            {
                IsCall = true;
            }
        }


        public class TestClass
        {
            [Fact]
            public void Class_1_Test()
            {
                var foo = Mock.Create<Class1>();
                Mock.Arrange(() => (foo as Class2).Method1()).CallOriginal().OccursOnce();
                Mock.Arrange(() => foo.Method1()).CallOriginal().OccursOnce();

                foo.Method1();

                Mock.Assert(foo);

                Assert.True(foo.IsCall);

            }
        } 
  2. Answer
    Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 27 Feb 2014 Link to this post

    Hi,

    Unfortunately, this is not possible in the current version of JustMock. It is impossible to arrange the base method of an overriden virtual method right now. I've logged an issue aboout this and it should be fixed in one of our upcoming releases. Please note, however, that intercepting base virtual methods (like Class1.Method1 in your example) will require the profiler to be enabled - it will not work with JustMock Lite.

    Unfortunately, I cannot provide you with a workaround. I suggest you test this specific behavior by means other than JustMock.

    Regards,
    Stefan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  3. Md.Hasanuzzaman
    Md.Hasanuzzaman avatar
    31 posts
    Member since:
    Apr 2011

    Posted 28 Feb 2014 in reply to Stefan Link to this post

    Thanks
  4. andrew
    andrew avatar
    1 posts
    Member since:
    Feb 2020

    Posted 28 Feb Link to this post

    Sorry to revive an old thread, trying to test some very similar code. Has there been a fix since for this. using ignore instance as a work around but that is not ideal.
  5. Dinko
    Admin
    Dinko avatar
    1109 posts

    Posted 03 Mar Link to this post

    Hi Andrew,

    We are working with limited resources because of the national holidays in Bulgaria and will need some more time to look into the case. We will get back to you as soon as we have more information regarding your case.

    Thank you for your understanding.

    Regards,
    Dinko
    Progress Telerik

    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 Feedback Portal and vote to affect the priority of the items
  6. Ivo
    Admin
    Ivo avatar
    32 posts

    Posted 04 Mar Link to this post

    Hello Andrew,

    JustMock is still having this limitation, as you mentioned the desired behavior can be achieved using future mocking, see below:

    public class UnitTest1
    {
        [Fact]
        public void TestUsingPartialMocking()
        {
            var foo = new Class1();
            Mock.Arrange(() => (foo as Class2).Method1()).IgnoreInstance().OccursOnce();
            Mock.Arrange(() => foo.Method1()).CallOriginal().OccursOnce();
    
            foo.Method1();
    
            Mock.Assert(foo);
            Mock.Assert((foo as Class2));
            Assert.True(foo.IsCall);
        }
    
        [Fact]
        public void TestUsingMockWithCallOriginalBehavior()
        {
            var foo = Mock.Create<Class1>(Behavior.CallOriginal);
            Mock.Arrange(() => (foo as Class2).Method1()).IgnoreInstance().OccursOnce();
            Mock.Arrange(() => foo.Method1()).CallOriginal().OccursOnce();
    
            foo.Method1();
    
            Mock.Assert(foo);
            Mock.Assert((foo as Class2));
            Assert.True(foo.IsCall);
        }
    }

    I hope that this information answers your questions.

    Regards,
    Ivo
    Progress Telerik

    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 Feedback Portal and vote to affect the priority of the items
Back to Top