में किसी अन्य विधि से क्लासए में विधि को कॉल किया गया है, तो परीक्षण करना संभव है कि Moq and dependency injection का उपयोग करके किसी विधि को कॉल किया गया है या नहीं। हालांकि, क्या यह जांचना संभव है कि कक्षा में एक विधि एक ही कक्षा में किसी अन्य को कॉल करे?क्लास ए
उदाहरण के लिए, मैं परीक्षण करना चाहता हूं कि अगर मैं एक निश्चित अपवाद लॉग करता हूं, तो एक सूचना संदेश भी लॉग होता है।
विधि है:
public void Error(string message, Exception exception, long logId = 0)
{
var int32 = (int)logId;
Info("Id was converted to an int so that it would fit in the log: " + logId, int32);
Error(message, exception, int32);
}
इस इकाई पर मेरे प्रयास यह परीक्षण किया गया था। परीक्षण विफल रहता है, क्या ऐसा कोई तरीका है कि यह किया जा सकता है?
void logging_an_error_with_a_long_id_also_logs_info()
{
var mock = new Mock<ILogger>();
var testedClass = new Logger();
var counter = 0;
testedClass.Error("test" + counter++, new Exception("test" + counter), Int64.MaxValue);
mock.Verify(m => m.Info(It.IsAny<string>(), It.IsAny<int>()));
}
के बाद से Info
और Error
तरीकों एक ही कक्षा (ClassA) में कर रहे हैं, मुझे नहीं लगता कि मैं ClassA में एक निर्भरता के रूप में ClassA पारित कर सकते हैं। तो क्या इसे परीक्षण की आवश्यकता नहीं है?
एक नज़र डालें [यहां] (http://stackoverflow.com/questions/1417048/how-to-verify-another-method-in-the-class-was-called-using-moq)। – dee