简体   繁体   English

如何通过NSubstitute监视方法返回值

[英]How to spy method return value by NSubstitute

I want to spy return value of a mocked method of a mocked interface in NSubstitute. 我想在NSubstitute中监视模拟接口的模拟方法的返回值。 I get return value of Received function but it always returns null . 我得到了Received函数的返回值,但始终返回null

public interface IFactory
{
    object Create();
}

public interface IMockable
{
    object SomeMethod();
}

public class Mockable : IMockable
{
    private readonly IFactory factory;

    public Mockable(IFactory factory)
    {
        this.factory = factory;
    }

    public object SomeMethod()
    {
        object newObject = factory.Create();
        return newObject;
    }
}

public class TestClass
{
    [Fact]
    void TestMethod()
    {
        var factory = Substitute.For<IFactory>();
        factory.Create().Returns(x => new object());
        IMockable mockable = new Mockable(factory);
        object mockableResult = mockable.SomeMethod();
        object factoryResult = factory.Received(1).Create();
        Assert.Equal(mockableResult, factoryResult);
    }
}

I expect that mockableResult and factoryResult be equal but factoryResult is null . 我希望mockableResultfactoryResult相等,但是factoryResultnull

That is because you are using it incorrectly. 那是因为您使用不正确。 Received is an assertion on the called method. Received是关于被调用方法的断言。 It does not return a usable value from mocked members. 它不会从模拟成员中返回可用值。

Review the follow modified version of your test that behaves as you would have expected. 查看行为符合预期的测试的以下修改版本。

public class TestClass {
    [Fact]
    void TestMethod() {
        //Arrange
        object factoryResult = new object(); //The expected result
        var factory = Substitute.For<IFactory>();
        factory.Create().Returns(x => factoryResult); //mocked factory should return this
        IMockable mockable = new Mockable(factory);

        //Act
        object mockableResult = mockable.SomeMethod(); //Invoke subject under test

        //Assert
        factory.Received(1).Create(); //assert expected behavior
        Assert.Equal(mockableResult, factoryResult); //objects should match
    }
}

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

相关问题 使用NSubstitute和Ninject返回值 - Using NSubstitute and Ninject to return a value 如何使用AutofacContrib.NSubstitute监视被测类 - How to spy the class under test with AutofacContrib.NSubstitute 如何使用 NSubstitute 模拟受保护的方法 - How to mock protected method with NSubstitute 从方法 NSubstitute 返回多个列表值 - Return multiple list values from method NSubstitute 使用 NSubstitute 模拟一个方法两次,首先抛出错误,然后返回相同的调用签名请求的值 - Mock a method twice with NSubstitute to first throw error and later return value for the same called signature request NSubstitute无法设置返回值(CouldNotSetReturnException) - NSubstitute cannot setup return value (CouldNotSetReturnException) 如何使用NSubstitute模拟对同一方法的调用序列以在AutoFixture中返回不同的值? - How to mock a sequence of calls to the same method to return different values in AutoFixture using NSubstitute? 如何在NSubstitute中使用Substitute参数返回对象 - How to return object in NSubstitute with Substitute parameters 如何配置NSubstitute NOT调用内部虚拟方法 - How to configure NSubstitute NOT call internal virtual method 如何通过NSubstitute中的超类匹配参数来存根方法? - How to stub a method by matching an argument by a superclass in NSubstitute?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM