向上转换到Moq上的接口不起作用

时间:2015-06-18 12:57:26

标签: c# unit-testing inheritance moq upcasting

我正在使用moq进行测试。我将对象转发到其界面并发现问题。

以下是显示问题的测试。

  • OBJECTS

    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; }
    }
    
  • TEST FAIL

    [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));
    }
    
  • TEST PASS

    [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投射到其中一个界面时,我仍然可以访问Model属性?

1 个答案:

答案 0 :(得分:0)

默认情况下,当您模拟具体类型时,Moq不会调用虚拟方法或实现接口。只有当你告诉它它可以调用基础实现时它才会这样做,否则它会假设你需要调用它们来设置这些方法。如果您将测试更改为以下内容,则可以使用:

[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));
}