c# - 这应该被 mock 还是被打断?

标签 c# unit-testing c#-4.0 dependency-injection moq

我有以下方法来测试,我已经编写了两个测试,测试抛出异常的场景,我想知道哪个是正确的。

namespace JimBob.CsvImporter.Entity
{

    public interface IIOManager
    {
        Stream OpenFile(string path);

        TextReader ReturnReader(string path);
    }


    public class IOManager : IIOManager
    {
        public Stream OpenFile(string path)
        {
            return File.Open(path, FileMode.Open);
        }

        public TextReader ReturnReader(string filePath)
        {
            return new StreamReader(filePath);
        }
    }


public class EntityVerification
{

    private IIOManager _iomgr;

    public EntityVerification(IIOManager ioManager)
    {
        this._iomgr = ioManager;
    }

    ...

    /// <summary>
    /// Ensures user can open file.
    /// </summary>
    /// <param name="errorMessageList">A running list of all errors encountered.</param>
    public void ValidateAccessToFile(string filePath, List<string> errorMessageList)
    {
        try
        {
            using (FileStream fs = (FileStream)_iomgr.OpenFile(filePath))
            {
                if (fs.CanRead && fs.CanWrite) { }
                else
                {
                    errorMessageList.Add("Can not read/write to the specified file.");
                }
            }
        }
        catch (Exception e)
        {
            errorMessageList.Add(e.Message);
        }
    }

测试:

    [Test]
    public void ValidateAccessToFile_CanReadWriteToFile_ThrowException()
    {
        List<String> errorMessageList = new List<string>();
        StubService stub = new StubService();
        EntityVerification testObject = new EntityVerification(stub);
        testObject.ValidateAccessToFile("ergesrg", errorMessageList);
        Assert.AreEqual(errorMessageList.Count, 0);
    }

    [Test]
    public void ValidateAccessToFile_CanReadWriteToFile_ThrowsException()
    {
        Mock<IIOManager> mock = new Mock<IIOManager>();
        mock.Setup(x => x.ReturnReader(It.IsAny<string>())).Throws(new InvalidOperation("throw baby."));
        EntityVerification testObject = new EntityVerification(mock.Object);
        List<String> errorMessageList = new List<string>();
        testObject.ValidateAccessToFile("blabla.txt", errorMessageList);
        Assert.AreEqual(errorMessageList.Count, 0);
    }



    public class StubService : IIOManager
    {
        public Exception ex;
        public Stream OpenFile(String path)
        {
            throw ex;
        }
    }

两个测试都只是检查测试的局部变量(在本例中为 errorMessageList)是否包含某些内容,因此我不确定应该使用哪个。

如有任何意见,我们将不胜感激。

谢谢

最佳答案

首先,您难道不应该检查一下您是否确实向列表中添加了错误消息吗?

Assert.AreEqual(errorMessageList.Count, 1);

第二,虽然第二个稍微不那么冗长,并且更具可读性(因为您不需要实现另一个类),没关系 - 这两个测试都是有效的实现同一目标的方法。只需选择一个,然后继续您的下一个功能...

关于c# - 这应该被 mock 还是被打断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11692165/

相关文章:

c# - 使用 C# 访问 Sharepoint - 在哪里可以找到引用库?

c# - 将基类转换为派生类

java - 使用 Mockito 2.0.7 模拟 lambda 表达式

scala - 将模拟 Actor 注入(inject) Spray 路线进行测试

c#-4.0 - 使用 Google API 从刷新 token 中获取访问 token

c# - NLog 在 Azure 中不起作用

c# - 如何在 WPF 中使用窗口作为初始屏幕?

python - 使用 Python 请求和响应模拟文件下载

linq - 如何显示来自xml文件的视频?

c#-4.0 - 是否可以使用 PayPal API Web 服务来处理 Website Payments Standard 付款?