c# - 我如何断言使用 NUnit 调用了特定方法?

标签 c# tdd nunit moq

作为测试结果,我如何测试使用正确参数调用特定方法?我正在使用 NUnit .

该方法不返回任何内容。它只是写在一个文件上。我正在为 System.IO.File 使用模拟对象。所以我想测试该函数是否被调用。

最佳答案

需要更多上下文。所以我将把一个添加到组合中的最小起订量放在这里:

pubilc class Calc {
    public int DoubleIt(string a) {
        return ToInt(a)*2;
    }

    public virtual int ToInt(string s) {
        return int.Parse(s);
    }
}

// The test:
var mock = new Mock<Calc>();
string parameterPassed = null;
mock.Setup(c => x.ToInt(It.Is.Any<int>())).Returns(3).Callback(s => parameterPassed = s);

mock.Object.DoubleIt("3");
Assert.AreEqual("3", parameterPassed);

关于c# - 我如何断言使用 NUnit 调用了特定方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1837933/

相关文章:

c# - 为什么 Entity Framework 没有在我的模型上正确保存枚举属性?

c# - 使用 PropertyInfo 中的新类型递归调用泛型方法?

c# - 检查 Elasticsearch 堆大小

ruby-on-rails - 如何在默认 Rails 中自定义测试生成器?

.net - 如何在 NUnit 2.5.6 中使用 : PartCover . NET 4

unit-testing - ReSharper错误: "The output has reached the limit and was truncated. To view the full output use ' Show Stack Trace in a new window' action.“

C# - 麦克风噪音检测

linq - LINQ 中的完整搜索测试(自定义过滤器查询)

c# - 使用固定装置测试 Entity Framework

c# - 如何做最小起订量可见的内部接口(interface)?