How to mock up appconfig value?

32 views
Skip to first unread message

Hui Zhao

unread,
Nov 9, 2015, 8:25:28 AM11/9/15
to Moq Discussions
Say I have 
public class FundsTransferAuthorization
    {
        public decimal MaxTransferableAmount { get; set; }
        public decimal MinTransferableAmount { get; set; }
        private ChargeAuthorization _chargeAuthorization;
        private readonly IPromptPlayer _promptPlayer;
        public FundsTransferAuthorization(IPromptPlayer promptPlayer)
        {
            _promptPlayer = promptPlayer;
            GetAuthorizationParameters();
        }

        private void GetAuthorizationParameters()
        {
            _maxDigit = _promptPlayer.MaxDigit;
             MaxTransferableAmount = Convert.ToDecimal(GetMaximumTransferableAmount());
            MinTransferableAmount = Convert.ToDecimal(GetMinimumTransferableAmount());
        }
        public virtual string GetMinimumTransferableAmount()
        {
            return ConfigurationManager.AppSettings["minimumTransferableAmount"];
        }
        public virtual string GetMaximumTransferableAmount()
        {
            return ConfigurationManager.AppSettings["maximumTransferableAmount"];
        }
I want to mock  up MaxTransferableAmount  and MinTransferableAmount rather than reading then from app.config, how to do that?

Thanks.

Emanuele DelBono

unread,
Nov 9, 2015, 12:27:17 PM11/9/15
to moq...@googlegroups.com
Since ConfigurationManager is static you cannot mock it.
So you have 2 possible solutions:
- Encapsulate ConfigurationManager inside a class that implement an interface
- Explicitly set the value that you need in your test
     ConfigurationManager.AppSettings["minimumTransferableAmount"] = "42"; )


bye


--
--
Post: moq...@googlegroups.com
Unsubscribe: moqdisc-u...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "Moq Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to moqdisc+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Hui Zhao

unread,
Nov 9, 2015, 4:37:46 PM11/9/15
to Moq Discussions
I tried the second one but why it is not working.
Reply all
Reply to author
Forward
0 new messages