C# 为 nUnit 测试创建委托静态方法的模拟
我正在研究 nUnit。我需要从主题下的同一类中设置静态方法的模拟。我想知道我是否正确创建了模拟,因为我定义了两个实例;被测试类的 sut 和同一类方法的模拟,以 MockObject.Setup(..) 为目标,它们存在于我试图测试的同一个类中。 GetNumbers() 方法的模拟
我需要帮助创建 ProcessCalculation 类
error
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("Testing Automation Proptype... ");
ProcessCalculation process = new ProcessCalculation();
process.Run();
Console.ReadLine();
}
}
计算
public class ProcessCalculation
{
public delegate List<int> GetNumbersPointer();
//GetNumbersPointer getNumbersPointer = new GetNumbersPointer(GetNumbers);
public GetNumbersPointer getNumbersPointer = GetNumbers;
public void Run()
{
var Nos = getNumbersPointer.Invoke();
int x = Nos[0];
int y = Nos[1];
var z = Add(x, y);
Console.WriteLine("sum "+ z);
}
public static int Add(int x, int y)
{
return x + y;
}
public static List<int> GetNumbers()
{
List<int> myNo = new List<int>();
myNo.Add(32);
myNo.Add(10);
return myNo;
}
}
测试类
public class Tests
{
private readonly ProcessCalculation sut;
private readonly Mock<ProcessCalculation> processCalculationMoq;
public Tests()
{
sut = new ProcessCalculation();
processCalculationMoq = new Mock<ProcessCalculation>();
}
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
//Arrange
var fixture = new Fixture();
var getNumberMoq = fixture.CreateMany<int>(2);
processCalculationMoq.Setup(x => x.getNumbersPointer).Returns(getNumberMoq); // this throw error.. I want to mock using setup
//???????????? how use delegate to mock static GetNumbers() method
//Assert
}
}
I am working on nUnit. I need to Setup mock of a static method from same class that is under subject. I am wonder If I created mock correctly as I have defined two instances; sut of class under test and mock of same class method in objective to MockObject.Setup(..) that exist in the same class that I am trying to test. I need help to create mock of GetNumbers()
method that is in ProcessCalculation class
error
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("Testing Automation Proptype... ");
ProcessCalculation process = new ProcessCalculation();
process.Run();
Console.ReadLine();
}
}
Calculation
public class ProcessCalculation
{
public delegate List<int> GetNumbersPointer();
//GetNumbersPointer getNumbersPointer = new GetNumbersPointer(GetNumbers);
public GetNumbersPointer getNumbersPointer = GetNumbers;
public void Run()
{
var Nos = getNumbersPointer.Invoke();
int x = Nos[0];
int y = Nos[1];
var z = Add(x, y);
Console.WriteLine("sum "+ z);
}
public static int Add(int x, int y)
{
return x + y;
}
public static List<int> GetNumbers()
{
List<int> myNo = new List<int>();
myNo.Add(32);
myNo.Add(10);
return myNo;
}
}
Test Class
public class Tests
{
private readonly ProcessCalculation sut;
private readonly Mock<ProcessCalculation> processCalculationMoq;
public Tests()
{
sut = new ProcessCalculation();
processCalculationMoq = new Mock<ProcessCalculation>();
}
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
//Arrange
var fixture = new Fixture();
var getNumberMoq = fixture.CreateMany<int>(2);
processCalculationMoq.Setup(x => x.getNumbersPointer).Returns(getNumberMoq); // this throw error.. I want to mock using setup
//???????????? how use delegate to mock static GetNumbers() method
//Assert
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的实现更改为
然后,在您的测试中:
Change your implementation to
And then, in your test: