gpt4 book ai didi

c# - 升级到 Moq 上的接口(interface)不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 15:38:46 27 4
gpt4 key购买 nike

我正在使用 moq为了我的测试。我将对象上传到它的接口(interface)并发现了一个问题。

下面是显示问题的测试。

  • 对象

    public interface IVehicle { string Model { get; } }
    public interface ICar : IVehicle { }
    public class Golf : ICar
    {
    public Golf(string model)
    {
    this.Model = model;
    }

    public string Model { get; private set; }
    }
  • 测试失败

    [TestMethod]
    public void InheritanceInterface_Test_WithMoq()
    {
    string golfmodel = "gti";
    var gti = new Moq.Mock<Golf>(golfmodel).Object;
    var safeVehicle = gti as IVehicle;
    var vehicle = (IVehicle)gti;
    var safeCar = gti as ICar;
    var car = (ICar)gti;

    Assert.AreEqual(golfmodel, gti.Model, string.Format(" Model:{0} | Golf Model:{1}", golfmodel, gti.Model));
    Assert.AreEqual(gti.Model, safeVehicle.Model, string.Format("Golf Model:{0} | Vehicle Model:{1}", gti.Model, safeVehicle.Model));
    Assert.AreEqual(gti.Model, vehicle.Model, string.Format("Golf Model:{0} | Vehicle Model:{1}", gti.Model, vehicle.Model));
    Assert.AreEqual(gti.Model, safeCar.Model, string.Format("Golf Model:{0} | Carro Model:{1}", gti.Model, safeCar.Model));
    Assert.AreEqual(gti.Model, car.Model, string.Format("Golf Model:{0} | Carro Model:{1}", gti.Model, car.Model));
    }
  • 测试通过

    [TestMethod]
    public void InheritanceInterface_Test()
    {
    string golfmodel = "gti";
    var gti = new Golf(golfmodel);
    var vehicle = (IVehicle)gti;
    var car = (ICar)gti;

    Assert.AreEqual(golfmodel, gti.Model, string.Format(" Model:{0} | Golf Model:{1}", golfmodel, gti.Model));
    Assert.AreEqual(gti.Model, vehicle.Model, string.Format("Golf Model:{0} | Vehicle Model:{1}", gti.Model, vehicle.Model));
    Assert.AreEqual(gti.Model, car.Model, string.Format("Golf Model:{0} | Carro Model:{1}", gti.Model, car.Model));
    }

我如何更改它以便在我施放 Mock<Golf>.Object 时到它的接口(interface)之一,我仍然可以访问 Model属性(property)?

最佳答案

默认情况下,当您 Mock 一个具体类型时,Moq 不会向下调用虚拟方法或实现接口(interface)。只有当您告诉它它可以调用基本实现时它才会这样做,否则它会假定您将在需要调用它们时设置这些方法。如果您将测试更改为以下内容,它将起作用:

[TestMethod]
public void InheritanceInterface_Test_WithMoq()
{
string golfmodel = "gti";
var golfMock = new Moq.Mock<Golf>(golfmodel);

golfMock.CallBase = true; // Tell the mock to call base methods

var gti = golfMock.Object;
var safeVehicle = gti as IVehicle;
var vehicle = (IVehicle)gti;
var safeCar = gti as ICar;
var car = (ICar)gti;

Assert.AreEqual(golfmodel, gti.Model, string.Format(" Model:{0} | Golf Model:{1}", golfmodel, gti.Model));
Assert.AreEqual(gti.Model, safeVehicle.Model, string.Format("Golf Model:{0} | Vehicle Model:{1}", gti.Model, safeVehicle.Model));
Assert.AreEqual(gti.Model, vehicle.Model, string.Format("Golf Model:{0} | Vehicle Model:{1}", gti.Model, vehicle.Model));
Assert.AreEqual(gti.Model, safeCar.Model, string.Format("Golf Model:{0} | Carro Model:{1}", gti.Model, safeCar.Model));
Assert.AreEqual(gti.Model, car.Model, string.Format("Golf Model:{0} | Carro Model:{1}", gti.Model, car.Model));
}

关于c# - 升级到 Moq 上的接口(interface)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30916001/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com