Automocking to properties

4 posts, 0 answers
  1. Adiel
    Adiel avatar
    60 posts
    Member since:
    Jun 2012

    Posted 27 Aug 2013 Link to this post

    Hi. 
    Is there any option to use MockingContainer to mock properties (not just the ctor)? 
    Thanks

  2. Tsvetomir
    Admin
    Tsvetomir avatar
    17 posts

    Posted 28 Aug 2013 Link to this post

    Hello Adiel,

    Yes, you can mock properties using MockingContainer. You can find additional information here http://www.telerik.com/help/justmock/properties_t_telerik_justmock_container_mockingcontainer_1.html .

    Here is an example of automocking where a mocked property (Balance) is used

    public class AccountService
            {
                public AccountService(IAcccount fromAccount, IAcccount toAccount)
                {
                    this.fromAccount = fromAccount;
                    this.toAccount = toAccount;
                }
     
                public void TransferFunds(decimal amount)
                {
                    if (fromAccount.Balance <= amount)
                    {
                        fromAccount.Withdraw(amount);
                        toAccount.Deposit(amount);
                    }
                }
     
                private IAcccount fromAccount;
                private IAcccount toAccount;
            }
     
            public interface IAcccount
            {
                decimal Balance { get; }
                void Withdraw(decimal amount);
                void Deposit(decimal amount);
            }
     
            [TestMethod]
            public void AutoMockingProperyExample()
            {
                var container = new MockingContainer<AccountService>();
     
                decimal expectedBalance = 100;
     
                container.Arrange<IAcccount>(x => x.Balance, Take.First()).Returns(expectedBalance);
                container.Arrange<IAcccount>(x => x.Withdraw(expectedBalance), Take.First()).MustBeCalled();
                container.Arrange<IAcccount>(x => x.Deposit(expectedBalance), Take.Last()).MustBeCalled();
     
                container.Instance.TransferFunds(expectedBalance);
     
                container.Assert();
            }

    Don't hesitate to contact us if you have other questions.

    Regards,
    Tsvetomir
    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. DevCraft R3 2016 release webinar banner
  4. Adiel
    Adiel avatar
    60 posts
    Member since:
    Jun 2012

    Posted 28 Aug 2013 Link to this post

    Hello Tsvetomir 
    This capability is clear. 
    My question was could I mock the interface if it's not a parameter of the ctor? 

    Regards
    Adiel
  5. Tsvetomir
    Admin
    Tsvetomir avatar
    17 posts

    Posted 28 Aug 2013 Link to this post

    Hi Adiel,

    I am sorry that I've misunderstood you. 

    The scenario you are asking me is not possible at the moment. For now we support only .ctor injection.

    We are working on a new feature, which will enable you to mock interface properties of future instances. This feature is scheduled for one of next internal builds of JustMock.

    Thank you for your time and cooperation.


    Regards,
    Tsvetomir
    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.
Back to Top