我在为对象上的 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; }
}
现在我想要 Equals of the Hours : 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
有人知道吗?