小编Dan*_*sen的帖子

单元测试时Stub vs Mock

我最近对测试非常感兴趣,我现在正努力学习以最佳方式进行单元测试.我和Ninoit一起使用Rhino Mocks.我在Stackoverflow上也经常阅读,但还没有找到我的问题的明确答案.

我想知道如果我有一个类似下面的方法,我应该模拟OfficeClass依赖项并测试GetAllOffices或仅使用存根来获取依赖关系并验证方法GetAllOffices已被调用,我确实得到了办公室,我从我的存根设置预期?

public Offices GetAllOffices() 
{
    try
    {
        var offices = officeClass.GetAllOffices();
        return offices;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果OfficeClass只是另一个POCO,或者是否因为嘲弄和存根而被称为网络服务,它会有什么不同吗?

长问题简介:何时进行模拟以及何时进行单元测试中的Stub?

c# unit-testing mocking stub

10
推荐指数
1
解决办法
2万
查看次数

什么时候在实例类中使用私有静态方法是有意义的

我继承了一些常规类的代码,其中包含一些私有静态方法.代码(伪代码)看起来像这样

public class Animal
{
    private string typeOfAnimal;

    public Animal(string typeOfAnimal)
    {
        this.typeOfAnimal = typeOfAnimal;
    }

    public void MakeSound()
    {
        var sound = Animal.GetSound(typeOfAnimal);

        // Make use of sound here       
    }

    private static string GetSound(string typeOfAnimal)
    {
        if(typeOfAnimal  == "dog")
            return "bark";
        else if(typeOfAnimal == "cat")
            return "mjau";
    }
}
Run Code Online (Sandbox Code Playgroud)

与使GetSound成为常规实例方法相比,这样做有什么好处?

c# static-methods private

5
推荐指数
1
解决办法
6326
查看次数

标签 统计

c# ×2

mocking ×1

private ×1

static-methods ×1

stub ×1

unit-testing ×1