尝试最小起订量 class 但无法访问非接口方法
Trying to moq a class but am unable to access non interfaced methods
假设我有一个接口
public interface IVehicle<T>
{
string Drive();
string Stop();
}
还有两个 classes Car() 和 Aeroplane()
每个都有一个class,它使用界面来执行操作
public class CarActions : IVehicle<Car>
{
public string Drive()
{
return "Go";
}
public string Stop()
{
return "Stop";
}
}
和
public class AeroplaneActions : IVehicle<Aeroplane>
{
public string Drive()
{
return "Go";
}
public string Stop()
{
return "Stop";
}
public virtual string Fly()
{
return "Fly";
}
}
当我模拟飞机 class 时,它找不到 fly() 方法,因为它不是接口的一部分
Mock<AeroplaneActions> mockedDirectly = new Mock<AeroplaneActions>();
mockedDirectly.Setup(method => method.Drive()).Returns("Drive");
mockedDirectly.Setup(method => method.Stop()).Returns("Stop");
mockedDirectly.Setup(method => method.Fly()).Returns("Fly");
我已经尝试直接模拟 Actions class 确实有效,但是在这种情况下我需要将我的方法更改为虚拟方法,我想避免这种情况。
Mock<AeroplaneActions> mockedDirectly = new Mock<AeroplaneActions>();
mockedDirectly.Setup(method => method.Drive()).Returns("Drive");
mockedDirectly.Setup(method => method.Stop()).Returns("Stop");
mockedDirectly.Setup(method => method.Fly()).Returns("Fly");
我想知道除了使用虚拟方法之外是否还有其他替代方法?
也许您可以为 AeroplaneActions
创建另一个接口,它将继承 IVehicle<T>
并具有 Fly
方法,例如像这样:
public interface IAeroplane<T> : IVehicle<T>
{
string Fly();
}.
那么classAeroplaneActions
会实现这个接口:
AeroplaneActions : IAeroplane<Aeroplane>
然后应该可以模拟这个接口:
var mock = new Mock<IAeroplane<Aeroplane>>().
假设我有一个接口
public interface IVehicle<T>
{
string Drive();
string Stop();
}
还有两个 classes Car() 和 Aeroplane()
每个都有一个class,它使用界面来执行操作
public class CarActions : IVehicle<Car>
{
public string Drive()
{
return "Go";
}
public string Stop()
{
return "Stop";
}
}
和
public class AeroplaneActions : IVehicle<Aeroplane>
{
public string Drive()
{
return "Go";
}
public string Stop()
{
return "Stop";
}
public virtual string Fly()
{
return "Fly";
}
}
当我模拟飞机 class 时,它找不到 fly() 方法,因为它不是接口的一部分
Mock<AeroplaneActions> mockedDirectly = new Mock<AeroplaneActions>();
mockedDirectly.Setup(method => method.Drive()).Returns("Drive");
mockedDirectly.Setup(method => method.Stop()).Returns("Stop");
mockedDirectly.Setup(method => method.Fly()).Returns("Fly");
我已经尝试直接模拟 Actions class 确实有效,但是在这种情况下我需要将我的方法更改为虚拟方法,我想避免这种情况。
Mock<AeroplaneActions> mockedDirectly = new Mock<AeroplaneActions>();
mockedDirectly.Setup(method => method.Drive()).Returns("Drive");
mockedDirectly.Setup(method => method.Stop()).Returns("Stop");
mockedDirectly.Setup(method => method.Fly()).Returns("Fly");
我想知道除了使用虚拟方法之外是否还有其他替代方法?
也许您可以为 AeroplaneActions
创建另一个接口,它将继承 IVehicle<T>
并具有 Fly
方法,例如像这样:
public interface IAeroplane<T> : IVehicle<T>
{
string Fly();
}.
那么classAeroplaneActions
会实现这个接口:
AeroplaneActions : IAeroplane<Aeroplane>
然后应该可以模拟这个接口:
var mock = new Mock<IAeroplane<Aeroplane>>().