如何使用MOQ使用类对象在OUT参数中

发布于 2025-02-08 05:43:38 字数 1730 浏览 2 评论 0原文

我试图嘲笑我的服务的一些功能。代码是这样的。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离旧人 2025-02-15 05:43:38

尝试“ MockiObj.Object”。这应该通过模拟的对象而不是模拟的实例

Try “mockIObj.Object” . This should pass in the mocked object rather than the mocked instance

绝影如岚 2025-02-15 05:43:38

我找到了解决此问题的其他方法。
此方法公共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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文