访问moq对象时如何返回null

访问moq对象时如何返回null

本文介绍了访问moq对象时如何返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Moq 库进行单元测试.现在我想要的是,当我第一次访问我的对象时,它应该返回 null,当我第二次访问它时,它应该返回其他内容.

I am using Moq library for unit testing. Now what i want is that when I access my object for the first time it should return null, and when i access this on second time it should return something else.

这是我的代码

var mock = new Mock<IMyClass>();
mock.Setup(?????);
mock.Setup(?????);

var actual = target.Method(mock.object);

在我的方法中,我首先检查模拟对象是否为空,如果为空,则对其进行初始化,然后对其进行一些调用.

in my method i am first checking that whether mock object is null or not, if it is null then do initialize it and then do some calls on it.

bool Method(IMyClass myObj)
{
    if (myObj != null)
        return true;
    else
    {
        myObj = new MyClass();
        bool result = myObj.SomeFunctionReturningBool();
        return result;
    }
}

如何设置模拟对象,

我还需要知道如何模拟这条线

Also i need to know how to mock this line

bool result = myObj.SomeFunctionReturningBool();

推荐答案

听起来您正在尝试使用一种测试方法运行两个测试 - 也许将测试拆分为两个会更好?

It sounds like you are trying to run two tests with one test method - maybe it would be better to split the tests into two?

如果方法传递空值,您还想初始化一个新对象.为了测试这一点,我建议创建一个工厂对象,负责创建 MyClass 的实例.新代码如下所示:

You also want to initialise a new object if the method is passed null. To test this, I suggest creating a factory object responsible for creating instances of MyClass. The new code would look like:

interface IMyClassFactory
{
    IMyClass CreateMyClass();
}

bool Method(IMyClass myObj, IMyClassFactory myClassFactory)
{
    if (myObj != null)
    {
        return true;
    }

    myObj = myClassFactory.CreateMyClass();
    return myObj.SomeFunctionReturningBool();
}

然后测试看起来像:

[Test]
public void Method_ShouldReturnTrueIfNotPassedNull()
{
    Assert.That(target.Method(new MyClass()), Is.True);
}

[Test]
public void Method_ShouldCreateObjectAndReturnResultOfSomeFunctionIfPassedNull()
{
    // Arrange
    bool expectedResult = false;

    var mockMyClass = new Mock<IMyClass>();
    mockMyClass.Setup(x => x.SomeFunctionReturningBool()).Returns(expectedResult);

    var mockMyFactory = new Mock<IMyClassFactory>();
    mockMyFactory.Setup(x => x.CreateMyClass()).Returns(mockMyClass.Object);

    // Act
    var result = target.Method(null, mockMyFactory.Object);

    // Assert
    mockMyClass.Verify(x => x.SomeFunctionReturningBool(), Times.Once());
    mockMyFactory.Verify(x => x.CreateMyClass(), Times.Once());
    Assert.That(result, Is.EqualTo(expectedResult));
}

这里使用了工厂模式来传入一个对象,该对象可以创建IMyClass 类型,然后工厂本身已经被模拟.

Here the factory pattern has been used to pass in an object which can create objects of IMyClass type, and then the factory itself has been mocked.

如果您不想更改方法的签名,请在类的构造函数中创建工厂,并使其可通过类的公共属性访问.然后它可以在测试中被模拟工厂覆盖.这称为依赖注入.

If you do not want to change your method's signature, then create the factory in the class's constructor, and make it accessible via a public property of the class. It can then be overwritten in the test by the mock factory. This is called dependency injection.

这篇关于访问moq对象时如何返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:53