chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Is it possible to Isolate a private class? For example with duck typing or anything? Without creating private accessors?

For example, how would I isolate the call to Logger.Write("") ???

public class SomeClass
{
   public void DoSomething(Customer customer)
   {
      if (customer.IsPaying)
      {
         Logger.Write("Let's start invoicing");
         Invoicing.StartInvoicing(customer.CustomerId);
      }
   }
}

private class Logger
{
   public static void Write(string msg)
   {
      throw new NotImplementedException();
   }
}
asked by dvdstelt (5.3k points)

12 Answers

0 votes
Unfortunately it's looking like you're going to need to post a more complete reproduction here or take it offline with Typemock Support. Without seeing an actual class hierarchy, complete with all of the base classes that things are inheriting from, and a full test that you actually want to debug, it's going to be difficult to provide you much more than I already have.

For example, given the snippets you just posted I can infer that you have a class structure that looks like this...

public class SomeGenericClass<T1>
{
  private class SomeInnerClass {}
}


But if that's the case, then the test snippet you posted won't actually compile:

Type someInnerClassDef = typeof( SomeGenericClass<T1>.SomeInnerClass );


It won't compile because the typeof() call won't let you specify the type parameter. It'd need to be:

typeof(SomeGenericClass<>.SomeInnerClass)


Without the type parameter specified. Given what you've posted, then, it's one of the following scenarios:

  • These aren't actual test snippets with just the type names changed (there may be some additional context in the test that you're not showing us that might help us troubleshoot)
  • You're specifying a real type in the typeof call (e.g., "System.String" and not just "T1"), meaning you're trying to mock a closed generic (which, if you are, could be the problem)
  • You really are specifying T1 in the typeof call and the method compiles, meaning the test is running inside a generic method or a generic test fixture (which, if you are, could be a problem)


In any case, I'm happy to help, but I think we probably need to see a full reproduction that actually compiles, runs, and fails. It's okay if it's anonymized and all of the method bodies just throw exceptions or whatever, as long as we can copy the code, paste it into a project, run the test, and see the same error you're seeing.
answered by tillig (6.7k points)
0 votes
You are right. I don't mean to make it harder to troubleshoot my exception. I am a not sure how much of our code my employer would be comfortable with me posting here. I will bring this up and post a more complete example soon. Thank you for your help.
answered by trp (4.1k points)
...