如何使用MOQ使用类对象在OUT参数中
我试图嘲笑我的服务的一些功能。代码是这样的。
public interface IObj {
public bool anotherMethod(string input, out string responseString);
}
public class SomeClass {
public bool SomeMethod(string input, out IObj outputObj) {
// some logic
if (logic is correct) {
outputObj = // object of IObj
return true;
}
outputObj = null;
return false;
}
}
public class Service {
public void executingMethod(){
if (this.someClassObj.SomeMethod(this.inputString, out outputObj) {
if (outputObj.anotherMethod(this.anotherInputString, out responseString)
{
// some business logic
}
}
}
}
现在,我想模拟使用MOQ和XUNIT的方法执行method
行为vai Untistest。 但是,在嘲笑时,我会遇到out
参数的问题。这样,我试图嘲笑行为。
[Fact]
public void MockingMethod(){
// Arrange
Mock<SomeClass> mockSomeClass = new Mock<SomeClass>();
Mock<IObj> mockIObj = new Mock<IObj>();
string mockedResponse = "someResponse";
// here i am getting the issue, as out is expecting actual object not mocked object.
mockSomeClass.Setup(s => s.SomeMethod(It.IsAny<string>(), out mockIObj).Returns(true);
mockIObj.Setup(s => s.anotherMethod(It.IsAny<string(), out mockedResponse).Returns(true);
}
任何帮助将不胜感激。 tia。
我也试图按照@Roman的建议使用。 mockSomeClass.Setup(s =&gt; s.SomeMeMethod(it.isany&lt; string&gt;(),out mockiObj.Object).Returns(true)(true);
;
,但它抛出了此错误 - &gt;属性或索引器不得以输出或参考参数传递”
I was trying to Mock some some functionality of my Services. The code goes like this.
public interface IObj {
public bool anotherMethod(string input, out string responseString);
}
public class SomeClass {
public bool SomeMethod(string input, out IObj outputObj) {
// some logic
if (logic is correct) {
outputObj = // object of IObj
return true;
}
outputObj = null;
return false;
}
}
public class Service {
public void executingMethod(){
if (this.someClassObj.SomeMethod(this.inputString, out outputObj) {
if (outputObj.anotherMethod(this.anotherInputString, out responseString)
{
// some business logic
}
}
}
}
Now i want to Mock the method executingMethod
behaviour vai UnitTest using Moq and xUnit.
But while mocking I am getting issue with out
parameter. In this way I am trying to mock the behaviour.
[Fact]
public void MockingMethod(){
// Arrange
Mock<SomeClass> mockSomeClass = new Mock<SomeClass>();
Mock<IObj> mockIObj = new Mock<IObj>();
string mockedResponse = "someResponse";
// here i am getting the issue, as out is expecting actual object not mocked object.
mockSomeClass.Setup(s => s.SomeMethod(It.IsAny<string>(), out mockIObj).Returns(true);
mockIObj.Setup(s => s.anotherMethod(It.IsAny<string(), out mockedResponse).Returns(true);
}
Any help will be much appreciated. TIA.
I tried to use as suggested by @Roman as well.mockSomeClass.Setup(s => s.SomeMethod(It.IsAny<string>(), out mockIObj.Object).Returns(true);
but it is throwing this error -> "A property or indexer may not be passed as out or ref parameter"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试“ MockiObj.Object”。这应该通过模拟的对象而不是模拟的实例
Try “mockIObj.Object” . This should pass in the mocked object rather than the mocked instance
我找到了解决此问题的其他方法。
此方法
公共bool somemethod(字符串输入,out iobj outputobj)
正在呼叫类似的方法
public bool oneanothermethod(字符串输入,字符串outputResponse)
模拟此方法解决问题。我不知道嘲笑内部呼叫可以在呼叫链上传播。
感谢您的所有帮助。
I Found other way to resolve this issue.
This method
public bool SomeMethod(string input, out IObj outputObj)
was calling one another method like this
public bool oneAnotherMethod(string input, out string outputResponse)
mocking this method resolve the issue.I was not knowing that mocking internal calls can propagate above the calling chain.
Thanks for all the help.