unit-testing - Resharper 和 Moq 返回方法

标签 unit-testing resharper moq

我有一个简单的模拟类 longRunningLibrary 我希望方法 Returns from Moq 输出一些文本(“此方法...”),您可以在以下代码部分中看到:

 _longRunningLibrary
   .Setup(lrl => lrl.RunForALongTime(30))
   .Returns("This method has been mocked!");

所以..我希望这段文本在 resharper 的单元测试 session 部分中输出,但我做不到。如何进行 ?

最佳答案

使用moq你可以这样做:

[Test]
public void MoqSample()
{
    var mock = new Mock<ILongRunner>();
    mock.Setup(lr => lr.RunForALongTime(It.IsAny<int>()))
        .Returns("This method has been mocked!");
    ILongRunner longRunner = mock.Object;
    Console.WriteLine(longRunner.RunForALongTime(1));
    Assert.AreEqual("This method has been mocked!", longRunner.RunForALongTime(2));
    Assert.Pass(longRunner.RunForALongTime(3));
}

public interface ILongRunner
{
    string RunForALongTime(int i);
}

使用 R# 测试运行器时可以使用 Console.WriteLine(...),不确定使用 VS 时是否如此。

Assert.Pass(...) 打印一条 Success: 消息

关于unit-testing - Resharper 和 Moq 返回方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17169040/

相关文章:

java - 如何使用 Mockito 测试 POST 方法

razor - ReSharper 支持自定义 Razor 指令

unit-testing - 为什么我的EventAggregator订阅不处理此事件?

javascript - 如何将 browserify 添加到 yeoman 项目中?

javascript - 如何使用 Jest 模拟 DynamoDBDocumentClient 构造函数(AWS 开发工具包 V3)

c# - 如何模拟 OracleConnection 和 OracleCommand?

c# - 使用 Moq 从另一个方法调用模拟方法

c# - 带有 ReSharper 8.2 的 VS2013 不识别代码契约(Contract)?

wpf - 如何使用 Resharper 使 XAML 编辑更具响应性

c# - Moq C# 内置类