moq
कॉल ऑर्डर को मान्य करना
खोज…
कॉल ऑर्डर को स्पष्ट रूप से मान्य करना
जहाँ परीक्षण की जाने वाली विधि एक कॉल से लेकर बाद की कॉल पर पास करने के लिए सूचनाओं का उपयोग करती है, एक तरीका जिसे यह सुनिश्चित करने के लिए इस्तेमाल किया जा सकता है कि इसे अपेक्षित क्रम में कहा जाता है, डेटा के इस प्रवाह को प्रतिबिंबित करने के लिए उम्मीदों को सेटअप करना है।
परीक्षण करने की विधि दी:
public void MethodToTest()
{
var str = _utility.GetInitialValue();
str = _utility.PrefixString(str);
str = _utility.ReverseString(str);
_target.DoStuff(str);
}
उम्मीदें से डेटा पारित करने के लिए सेट किया जा सकता GetInitialValue
के माध्यम से PrefixString
और ReverseString
को DoStuff
, जहां जानकारी सत्यापित है। यदि किसी भी विधि को क्रम से बाहर किया जाता है, तो अंतिम डेटा गलत होगा और परीक्षण विफल हो जाएगा।
// Create mocks
var utilityMock = new Mock<IUtility>();
var targetMock = new Mock<ITarget>();
// Setup expectations, note that the returns value from one call matches the expected
// parameter for the next call in the sequence of calls we're interested in.
utilityMock.Setup(x => x.GetInitialValue()).Returns("string");
utilityMock.Setup(x => x.PrefixString("string")).Returns("Prefix:string");
utilityMock.Setup(x => x.ReverseString("Prefix:string")).Returns("gnirts:xiferP");
string expectedFinalInput = "gnirts:xiferP";
// Invoke the method to test
var sut = new SystemUnderTest(utilityMock.Object, targetMock.Object);
sut.MethodToTest();
// Validate that the final call was passed the expected value.
targetMock.Verify(x => x.DoStuff(expectedFinalInput));
कॉलबैक के साथ वैध कॉल ऑर्डर
जब आप स्ट्रिक्ट MockSequence
का उपयोग नहीं कर सकते / करना चाहते हैं, तो आप कॉल ऑर्डर को मान्य करने के लिए MockSequence
का उपयोग नहीं कर सकते। एक वैकल्पिक दृष्टिकोण यह सुनिश्चित करने के लिए कॉलबैक का उपयोग करना है कि Setup
अपेक्षाओं को अपेक्षित क्रम में लागू किया जा रहा है। परीक्षण करने के लिए निम्नलिखित विधि दी गई है:
public void MethodToTest()
{
_utility.Operation1("1111");
_utility.Operation3("3333");
_utility.Operation2("2222");
}
इसे निम्नानुसार परखा जा सकता है:
// Create the mock (doesn't have to be in strict mode)
var utilityMock = new Mock<IUtility>();
// Create a variable to track the current call number
int callOrder = 1;
// Setup each call in the sequence to be tested. Note that the callback validates that
// that callOrder has the expected value, then increments it in preparation for the next
// call.
utilityMock.Setup(x => x.Operation1(It.IsAny<string>()))
.Callback(() => Assert.AreEqual(1, callOrder++, "Method called out of order") );
utilityMock.Setup(x => x.Operation2(It.IsAny<string>()))
.Callback(() => Assert.AreEqual(2, callOrder++, "Method called out of order") );
utilityMock.Setup(x => x.Operation3(It.IsAny<string>()))
.Callback(() => Assert.AreEqual(3, callOrder++, "Method called out of order") );
// Invoke the method to be tested
var sut = new SystemUnderTest(utilityMock.Object);
sut.MethodToTest();
// Validate any parameters that are important, note these Verifications can occur in any
// order.
utilityMock.Verify(x => x.Operation2("2222"));
utilityMock.Verify(x => x.Operation1("1111"));
utilityMock.Verify(x => x.Operation3("3333"));
MockSequence के साथ कॉल ऑर्डर को मान्य करना
Moq, MockSequence
का उपयोग करके कॉल ऑर्डर को मान्य करने के लिए समर्थन प्रदान करता है, हालांकि यह केवल सख्त MockSequence
का उपयोग करते समय काम करता है। तो, परीक्षण करने के लिए निम्न विधि को देखते हुए:
public void MethodToTest()
{
_utility.Operation1("1111");
_utility.Operation2("2222");
_utility.Operation3("3333");
}
इसे निम्नानुसार परखा जा सकता है:
// Create the mock, not MockBehavior.Strict tells the mock how to behave
var utilityMock = new Mock<IUtility>(MockBehavior.Strict);
// Create the MockSequence to validate the call order
var sequence = new MockSequence();
// Create the expectations, notice that the Setup is called via InSequence
utilityMock.InSequence(sequence).Setup(x => x.Operation1(It.IsAny<string>()));
utilityMock.InSequence(sequence).Setup(x => x.Operation2(It.IsAny<string>()));
utilityMock.InSequence(sequence).Setup(x => x.Operation3(It.IsAny<string>()));
// Run the method to be tested
var sut = new SystemUnderTest(utilityMock.Object);
sut.MethodToTest();
// Verify any parameters that are cared about to the operation being orchestrated.
// Note that the Verify calls can be in any order
utilityMock.Verify(x => x.Operation2("2222"));
utilityMock.Verify(x => x.Operation1("1111"));
utilityMock.Verify(x => x.Operation3("3333"));
उपरोक्त उदाहरण उम्मीदों को स्थापित करते समय It.IsAny<string>
का उपयोग करता है। यदि अधिक सटीक मैचों की आवश्यकता होती है, तो वे प्रासंगिक स्ट्रिंग्स ("1111", "2222", "3333") का उपयोग कर सकते थे।
जब कॉल आउट ऑफ सीक्वेंस किए जाते हैं तो रिपोर्ट की गई त्रुटि थोड़ी भ्रामक हो सकती है।
आह्वान नकली व्यवहार के साथ विफल रहा। मॉक पर सभी इनवोकेशन में एक समान सेटअप होना चाहिए।
इसका कारण यह है, प्रत्येक Setup
अपेक्षा को माना जाता है जैसे कि यह तब तक मौजूद नहीं है जब तक कि अनुक्रम में पिछली अपेक्षा संतुष्ट नहीं हुई है।