The short answer is that you can't. This would require the ability to mock the new operator. typemock is an expensive alternative to moq that let's you mock things like the new operator. Really you need to restructure your code to avoid creational patterns. Chad Meyers wrote a great explanation on his blog
http://lostechies.com/chadmyers/2010/02/13/composition-versus-inheritance/
--Tim
--
Post: moq...@googlegroups.com
Unsubscribe: moqdisc-u...@googlegroups.com
Hello Vabhu,
How about if you restructure the code a bit. There seems to be a tight coupling. CallingClass is tightly coupled with ABC and MyClass.
You can use either constructor/property injection.
How about if you restructure CallingClass as.
public class CallingClass
{
private ABC _abc;
private MyClass _obj;
public CallingClass(ABC abc, MyClass obj)
{
//depending on your requirement, you might throw if null comes in for either of them. But, that all will depend on your requirement.
_abc=abc;
_obj = obj;
}
public void callingMethod(int x)
{
// you might check for null here especially if your constructor allows null to be set to _abc and _obj. But, still - this shouldn't affect your testability.
_abc.a =x;
_obj.changeObjectState(abc);
}
}
As you can see, no logic changed at all. Just a little restructure of the code. But, this time, it's much easier for you to create a mocked instance of ABC and MyClass and use those to test CallingClass. Just inject those mocked instances to CallingClass's constructor and you will have an instance to do your tests against.
This way, your class (CallingClass) is NOT tide up with only one implementation.
Let me know if this helps,
Thanks,
Bsharper