In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you the "Moq with ref parameter method of how to use Callback", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "Moq with ref parameter method of Callback how to use" this article.
With the popularity of mock technology in the .NET world, Moq has also become popular, in part because it is a class library tailored for developers who are new to mock technology or need to write custom mock objects. Instead of the classic Record/Reply paradigm, Moq lets testers use Lambda expressions to set the expected results of the behavior and use Castle DynamicProxy to truncate calls to mock objects.
In recent use, I feel a lot of pressure when the parameter of the method of the mock object comes with the ref keyword.
First of all, to recreate the crime scene, first define the interface we need to mock:
Public interface ITestInterface {string TestMethodWithRef (ref string refStr, string str);}
Next, we mock the method TestMethodWithRef of our defined interface and specify that the delegate operation is performed after the method is called:
[TestMethod] public void Ref_Param_Test () {var mock = new Mock (); string refStr = "1"; string str = "2"; mock.Setup ((m) = > m.TestMethodWithRef (ref refStr, str)). Callback ((string rs, string s) = > Console.WriteLine (rs + s); mock.Object.TestMethodWithRef (ref refStr, str); mock.VerifyAll ();}
There seems to be no problem with the above test method, and there is no problem with compilation, but a tragedy occurs when the test is run, and an exception is thrown
System.ArgumentException: Invalid callback. Setup on method with parameters (String&,String) cannot invoke callback with parameters (String,String)
This exception means that the parameters of the method executed by the Callback delegate do not correspond to those of the Setup method. Some people may immediately want to say that it is not necessary to change it:
Mock.Setup ((m) = > m.TestMethodWithRef (ref refStr, str)) .Callback ((ref string rs, string s) = > Console.WriteLine (rs + s))
Unfortunately, the Microsoft boss directly told you that the parameters in lamada expressions cannot be used with ref and out:
Variables introduced within a lambda expression are not visible in the outer method
Now the pressure is really big, calm down, believe in google! After looking for a lot of information, it is a pity that there are only two solutions that feel useful. * is very straightforward, don't use Moq to forge objects, and directly type your own code to forge interfaces or objects and related methods, but it feels like this solution is a bit tricky. The second is that do not pass parameters in the delegated operation:
Mock.Setup ((m) = > m.TestMethodWithRef (ref refStr, str)). Callback () = > Console.WriteLine (refStr + str). Returns ("). Verifiable ()
How to say that the second scheme is quite satisfactory, at least it can solve most of the problems.
It's almost over, but I accidentally stepped on another hole. Let's modify our unit testing method:
[TestMethod] public void Ref_Param_Test () {var mock = new Mock (); string refStr = "1"; string str = "1"; mock.Setup ((m) = > m.TestMethodWithRef (ref refStr, str)). Callback () = > {refStr = "2"; str = "2";}). Returns ("). Verifiable (); mock.Object.TestMethodWithRef (ref refStr, str); mock.VerifyAll (); Assert.AreEqual (" 2 ", str) Assert.AreEqual ("2", refStr);}
Looking directly at the logic of this test, I think most people will think that there is nothing wrong with it?
Still do not rest assured, run it, the tragedy continues to happen, the test failed: Assert.AreEqual failed. It should be:, actually:
The value of the variable refStr is still "1", which is really interesting!
The above is all the content of the article "how to use Callback with ref parameter method in Moq". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.