相关疑难解决方法(0)

487
推荐指数
6
解决办法
21万
查看次数

moq callbase用于不返回值的方法(void方法)

我正在尝试模拟我正在测试的类,以便在测试它们时可以调用各个方法的基础.这将允许我仅将方法设置测试为callbase,并且将模拟在测试方法中调用的所有其他方法(同一类).

但是,我无法为不返回值的方法执行此操作.对于不返回值的方法,intellisense不显示callbase的选项.

这可能吗?

服务类:

public class Service
{
    public Service()
    {
    }

    public virtual void StartProcess()
    {
        //do some work
        string ref = GetReference(id);
        //do more work
        SendReport();
    }

    public virtual string GetReference(int id)
    {
        //...
    }

    public virtual void SendReport()
    {
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

测试类设置:

var fakeService = new Mock<Service>();
fakeService.Setup(x => x.StartProcess());
fakeService.Setup(x => x.GetReference(It.IsAny<int>())).Returns(string.Empty);
fakeService.Setup(x => SendReport());
fakeService.CallBase = true;
Run Code Online (Sandbox Code Playgroud)

现在在我测试GetReference的测试方法中,我可以这样做:

fakeService.Setup(x => x.GetReference(It.IsAny<int>())).CallBase();
Run Code Online (Sandbox Code Playgroud)

但是当我想为StartProcess做同样的事情时,.CallBase就不存在了:

fakeService.Setup(x => x.StartProcess()).CallBase();
Run Code Online (Sandbox Code Playgroud)

一旦我使方法返回一些东西,这样一个布尔值,它就变得可用.

tdd unit-testing moq mocking

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

标签 统计

mocking ×2

unit-testing ×2

moq ×1

tdd ×1