简体   繁体   English

为什么即使我在 NSubstitute 中安排方法,它总是返回 Null 结果?

[英]why does it always return Null result even I Arrange the method in NSubstitute?

I try to Arrange a factory to return a service instance.我尝试安排一个工厂返回一个服务实例。

And then I arrange this service, however the method in the service always return null, even I arrange the mehod, why?然后我安排了这个服务,但是服务中的方法总是返回null,即使我安排了方法,为什么?

Here is my sample code, could somebody help me on this?这是我的示例代码,有人可以帮我吗? Thanks in advance!提前致谢!

using Moq;
using NSubstitute;
using Xunit;

namespace ConsoleApp1
{
    public interface IFactory
    {
        IMyService CreateInService(int number);
    }
    public  interface IMyService
    {
        (Status?,Status?)GetName(string name);
    }
    
    public enum Status
    {
        UnKnown = 0,
    }
    
    public class TupleTest
    {

        [Fact]
        public void GetTupleTest()
        {
            (Status?,Status?) exceptedStatusTuple = (Status.UnKnown,Status.UnKnown);
            
            var factory = Substitute.For<IFactory>();
            var myService = Substitute.For<IMyService>();

            factory.CreateInService(It.IsAny<int>()).Returns(myService);
            myService.GetName(It.IsAny<string>()).ReturnsForAnyArgs(exceptedStatusTuple);

            var result = factory.CreateInService(100).GetName("test");
            
            //result.item1 is null, it should be Status.UnKnown!
            Assert.Equal(result.Item1,exceptedStatusTuple.Item1);
            //result.item2 is null,it should be Status.UnKnown!
            Assert.Equal(result.Item2,exceptedStatusTuple.Item2);
        }
    }
}

It looks like you are mixing up Moq and NSubstitute calls.看起来您正在混淆起订量和 NSubstitute 调用。 Try commenting out the using Moq;尝试注释掉using Moq; line and fix the compile errors with that test.行并使用该测试修复编译错误。

One example I can see is It.IsAny is a Moq call.我可以看到的一个例子是It.IsAny是一个起订量调用。 It will need to be Arg.Any for NSubstitute.对于 NSubstitute,它需要是Arg.Any

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM