Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing Resharper和Moq返回方法_Unit Testing_Resharper_Moq - Fatal编程技术网

Unit testing Resharper和Moq返回方法

Unit testing Resharper和Moq返回方法,unit-testing,resharper,moq,Unit Testing,Resharper,Moq,我有一个简单的模拟类longRunningLibrary,我希望该方法从Moq返回以输出一些文本(“此方法…”),您可以在下面的代码部分中看到: _longRunningLibrary .Setup(lrl => lrl.RunForALongTime(30)) .Returns("This method has been mocked!"); 所以。。我想在resharper的单元测试会话部分输出这个文本,但我不能这样做。如何进行 使用以下命令可以执行以下操作: [Tes

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

 _longRunningLibrary
   .Setup(lrl => lrl.RunForALongTime(30))
   .Returns("This method has been mocked!");
所以。。我想在resharper的单元测试会话部分输出这个文本,但我不能这样做。如何进行

使用以下命令可以执行以下操作:

[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);
}
[测试]
公共无效MoqSample()
{
var mock=new mock();
mock.Setup(lr=>lr.runforelongtime(It.IsAny()))
.Returns(“此方法已被模拟!”);
ILongRunner longRunner=mock.Object;
Console.WriteLine(longRunner.runforelongtime(1));
AreEqual(“此方法已被模拟!”,longRunner.RunForALongTime(2));
Assert.Pass(longRunner.runforelongtime(3));
}
公共接口ILongRunner
{
字符串长时间运行(int i);
}
在使用R#testrunner时可以使用Console.WriteLine(…),但在使用VS时不能确定

Assert.Pass(…)使用打印成功:消息,您可以这样做:

[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);
}
[测试]
公共无效MoqSample()
{
var mock=new mock();
mock.Setup(lr=>lr.runforelongtime(It.IsAny()))
.Returns(“此方法已被模拟!”);
ILongRunner longRunner=mock.Object;
Console.WriteLine(longRunner.runforelongtime(1));
AreEqual(“此方法已被模拟!”,longRunner.RunForALongTime(2));
Assert.Pass(longRunner.runforelongtime(3));
}
公共接口ILongRunner
{
字符串长时间运行(int i);
}
在使用R#testrunner时可以使用Console.WriteLine(…),但在使用VS时不能确定


Assert.Pass(…)打印成功消息

另一个选项是使用回调:

_longRunningLibrary
   .Setup(lrl => lrl.RunForALongTime(30))
   .Returns("This method has been mocked!")
   .Callback<int>( p => Console.WriteLine("Called with: {0}", p);
\u龙润宁图书馆
.Setup(lrl=>lrl.runforLongtime(30))
.Returns(“此方法已被模拟!”)
.Callback(p=>Console.WriteLine(“用:{0}调用”,p);

另一种选择是使用回调:

_longRunningLibrary
   .Setup(lrl => lrl.RunForALongTime(30))
   .Returns("This method has been mocked!")
   .Callback<int>( p => Console.WriteLine("Called with: {0}", p);
\u龙润宁图书馆
.Setup(lrl=>lrl.runforLongtime(30))
.Returns(“此方法已被模拟!”)
.Callback(p=>Console.WriteLine(“用:{0}调用”,p);

谢谢你,约翰,我现在就用这个。我还在R#论坛上问了一个类似的问题。谢谢你,约翰,我现在就用这个。我也在R#论坛上问了一个类似的问题。