0
  BTPhysicalAccount testerInstance = new BTPhysicalAccount(IvirtualAccounts, BTmarketIn, int);

I want to test a method of this class. that BTmarketIn is a concrete class and a method in this class is used in that method under test. So I needed to mock that method hence i tried this:

        var readerAsk = Substitute.ForPartsOf<BTMarketInfo>();
        readerAsk.When(x => x.GetAskPrice(Arg.Any<SymbolType>())).DoNotCallBase();
        readerAsk.GetAskPrice(Arg.Any<SymbolType>()).Returns(2000);
        readerAsk.When(x => x.GetBidPrice(Arg.Any<SymbolType>())).DoNotCallBase();
        readerAsk.GetBidPrice(Arg.Any<SymbolType>()).Returns(2000);

it kept throwing null exception error and after some research I realized that you cannot mock non-virtual methods. So I do have an interface class of that BTMarketIn class IMarketInfo. Now I can mock an instance of this interface but I need an instance of BTMarketIn to pass as constructor argument. How do I resolve this issue?

Usama Aslam
  • 437
  • 7
  • 18
  • Can you refactor to pass an `IMarketInfo` into whatever constructor you're referring to? Currently, it sounds like `BTMarketInfo` and the class you're testing are tightly coupled. If you can refactor the target class to take an `IMarketInfo` into the constructor rather than a `BTMarketInfo`, you'll be in better shape. – wablab Dec 01 '16 at 14:33
  • Nope refactoring is not an option, otherwise I could simply make methods virtual and mock them. Is there no other way? – Usama Aslam Dec 01 '16 at 14:41
  • You could possibly use Microsoft Fakes Shims: https://msdn.microsoft.com/en-us/library/hh549175.aspx#shims. I don't have any experience with them, but I think this is the use case they're intended for. – wablab Dec 01 '16 at 14:47
  • Thank you. It really worked. – Usama Aslam Dec 06 '16 at 08:40
  • Glad it worked. I've posted the comment as an answer so that you can accept it to close out the question. Thanks! – wablab Dec 06 '16 at 14:39

1 Answers1

0

Microsoft Fakes Shims should work for this: https://msdn.microsoft.com/en-us/library/hh549175.aspx#shims

wablab
  • 1,703
  • 13
  • 15