[英]How to modify the value of passed in parameter in Moq
I am writing the unit test using moq for a method which has a parameter passed by ref. 我正在使用moq编写单元测试,该方法的参数由ref传递。 I need to modify the value of that parameter after the method is called.
调用方法后,我需要修改该参数的值。 I am trying using the Callback for Moq setup but it is not updating the value.
我正在尝试使用Moq设置的回调,但它没有更新该值。
myMock = new Mock<IMyObject>();
Foo foo = null;
myMock.Setup(x => x.GetResult(It.IsAny<string>(), ref foo))
.Returns("SUCCESS")
.Callback(() => { foo = new Foo ();});
I seen similar question here but they are answered to use callback, but I get the value of foo as null always. 我在这里看到了类似的问题,但是它们被回答使用回调,但是我总是将foo的值设置为null。
I was struggling with a similar issue for the past 12 hours and the only way I achieve the results is by manually inheriting and mocking the object. 在过去的12个小时中,我一直在努力解决类似的问题,而获得结果的唯一方法是手动继承和模拟对象。
In your case you can setup a fake object that inherits IMyObject and define it's .GetResult behavior to initialize a new Foo object and then return a "SUCCESS". 在您的情况下,您可以设置一个继承IMyObject的假对象,并将其定义为.GetResult行为以初始化新的Foo对象,然后返回“ SUCCESS”。
You can then use the NUnit api for asserts. 然后,您可以使用NUnit api进行断言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.