我在设置对象上的Equals方法测试时遇到了问题。

有问题的对象由此界面定义:

public interface IHours {
    ITimeOfDay OpenAt { get; set; }
    ITimeOfDay CloseAt { get; set; }
    DateTime ValidFrom { get; set; }
    DateTime ValidTo { get; set; }
    bool isCovered(DateTime time);
}

它包含对ITimeOfDay定义的引用:

public interface ITimeOfDay {
    DateTime Time { get; set; }
    int Hour { get; }
    int Minute { get; }
    int Second { get; }
}

现在我想要等待时间:IHours检查OpenAt和CloseAt IHours。为了进行设置,我尝试将这些属性值存根,并根据我的特定测试需要返回true和false。

    [SetUp]
    public virtual void SetUp() {
        mocks = new MockRepository();

        defaultHours = getHours();
        otherHours = getHours();

    }

    [TearDown]
    public virtual void TearDown() {
        mocks.ReplayAll();
        mocks.VerifyAll();
    }

    [Test(Description = "Equals on two Hours should regard the fields")]
    public void Equals_TwoValueEqualledObjects_Equal() {
        var openAt = mocks.Stub<ITimeOfDay>();
        var closeAt = mocks.Stub<ITimeOfDay>();

        closeAt                                   //this is line 66, referenced in the error stacktrace
            .Stub(o => o.Equals(null))
            .IgnoreArguments()
            .Return(true);

        openAt
            .Stub(c => c.Equals(null))
            .IgnoreArguments()
            .Return(true);
        mocks.ReplayAll();

        defaultHours.OpenAt = openAt;
        otherHours.OpenAt = openAt;

        defaultHours.CloseAt = closeAt;
        defaultHours.CloseAt = closeAt;

        Assert.That(defaultHours, Is.EqualTo(otherHours));
        Assert.That(defaultHours.GetHashCode(), Is.EqualTo(otherHours.GetHashCode()));
    }

但是当我运行它时,我得到了这个神秘的错误:

System.InvalidOperationException: Type 'System.Boolean' doesn't match the return type   'System.Collections.Generic.IList`1[NOIS.Model.Interfaces.IAircraft]' for method 'IAircraftType.get_Aircrafts();'
at Rhino.Mocks.Expectations.AbstractExpectation.AssertTypesMatches(Object value)
at Rhino.Mocks.Expectations.AbstractExpectation.set_ReturnValue(Object value)
at Rhino.Mocks.Impl.MethodOptions`1.Return(T objToReturn)
at Nois.Test.Model.Entities.HoursTest.Equals_TwoValueEqualledObjects_Equal() in HoursTest.cs: line 66 

IAircraftType接口是同一名称空间的一部分,但在测试,接口或实现类中没有引用它。我不明白它干扰的原因。据我所知,没有提及它。

我正在使用 - Rhino.Mocks v3.5.0.1337 - NUnit.Framework v2.5.0.8332

有人有任何想法吗?

有帮助吗?

解决方案

是的,这很复杂 - 错误是疯狂的,这与IAircraft无关。基本上问题是接口不是类,因此不从对象继承。换句话说 - closeAt没有一个Equals方法来存根。

事实上,它可能是一种语言流,你甚至可以在一个对象上显式调用Equals()。

解决这个问题的两种方法

  1. 不要模拟界面,模拟实现mocks.Stub() - 这确实有一个虚拟的equals方法,所以你的代码可以工作。
  2. 更好的是,在界面中添加Equals方法。一旦你这样做,你将能够覆盖它,因为所有类都从对象继承,你不必永远地实现它(除非你想)。
  3. 换句话说

    var intrface = MockRepository.GenerateStub<IInterface>();
    intrface.Stub(x => x.Equals(null)).IgnoreArguments().Return(true); 
    

    时休息
    public interface IInterface {
    }
    

    时有效
    public interface IInterface {
      bool Equals(object obj);
    }
    

其他提示

除非我遗漏了什么,否则时间应该是一个简单的不可变对象。所以我只是将它实现为一个小的,经过测试的价值类。然后你可以使用真正的Equals方法。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top