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

Automocking to properties

3 Answers 76 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Adiel
Top achievements
Rank 2
Adiel asked on 27 Aug 2013, 03:35 PM
Hi. 
Is there any option to use MockingContainer to mock properties (not just the ctor)? 
Thanks

3 Answers, 1 is accepted

Sort by
0
Tsvetomir
Telerik team
answered on 28 Aug 2013, 10:57 AM
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.
0
Adiel
Top achievements
Rank 2
answered on 28 Aug 2013, 11:36 AM
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
0
Tsvetomir
Telerik team
answered on 28 Aug 2013, 01:00 PM
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.
Tags
General Discussions
Asked by
Adiel
Top achievements
Rank 2
Answers by
Tsvetomir
Telerik team
Adiel
Top achievements
Rank 2
Share this question
or