我是单元测试的新手,我想知道这是如何解决的,我想这是典型的,通常可以解决的问题:
我有要测试的受保护方法。我已经用测试类覆盖了测试类,但是测试类的构造函数有 4 个参数,没有默认构造函数。您在测试类的哪个部分添加了对基(4 个参数)构造函数的调用?我已经尝试过[SetUp]
方法,但我得到了Use of keyword 'base' is not valid in this context
错误。
我认为这个简单的案例是自我解释的,但这里是一个例子:
public class A
{
protected Class1 obj1;
protected Class2 obj2;
protected Class3 obj3;
protected Class4 obj4;
public A(Class1 obj1, Class2 obj2, Class3 obj3, Class4 obj4)
{
this.obj1 = obj1;
this.obj2 = obj2;
this.obj3 = obj3;
this.obj4 = obj4;
}
protected virtual void MethodA()
{
//some logic
}
}
[TestFixture]
public class ATests : A
{
[SetUp]
public void SetUp()
{
this.obj1 = Substitute.For<Class1>();
this.obj2 = Substitute.For<Class2>();
this.obj3 = Substitute.For<Class3>();
this.obj4 = Substitute.For<Class4>();
//this line below doesn't compile with the `Use of keyword 'base' is not valid in this context` error.
base(obj1, obj2, obj3, obj4);
}
[Test]
public void MethodA_test()
{
//test MethodA logic
}
}