C# 如何告诉抽象类的mock/stub使用其对Object.Equals()的重写?
我有一个相对简单的抽象类。为了这个问题,我把它进一步简化了C# 如何告诉抽象类的mock/stub使用其对Object.Equals()的重写?,c#,unit-testing,mstest,rhino-mocks,equals,C#,Unit Testing,Mstest,Rhino Mocks,Equals,我有一个相对简单的抽象类。为了这个问题,我把它进一步简化了 public abstract class BaseFoo { public abstract string Identification { get; } //some other abstract methods public override bool Equals(object obj) { BaseFoo other = obj as BaseFoo; if(o
public abstract class BaseFoo
{
public abstract string Identification { get; }
//some other abstract methods
public override bool Equals(object obj)
{
BaseFoo other = obj as BaseFoo;
if(other == null)
{
return false;
}
return this.Identification.Equals(other.Identification);
}
}
我正试图找出如何编写一个单元测试来确保objectequals覆盖有效。我尝试创建一个mock,但当我将mock转换为对象并调用Equals时,它不会调用抽象类中的代码。它只是立即返回false。如果我将其添加到对象列表并在列表上调用.Remove或.Contains,则相同;仍然只是返回false,而没有命中抽象类中的代码
我正在使用mstest和rhino Mock
为了完整性,这里有一个测试,我希望它能工作,但不能:
[TestMethod]
public void BaseFoo_object_Equals_returns_true_for_Foos_with_same_Identification()
{
var id = "testId";
var foo1 = MockRepository.GenerateStub<BaseFoo>();
var foo2 = MockRepository.GenerateStub<BaseFoo>();
foo1.Stub(x => x.Identification).Return(id);
foo2.Stub(x => x.Identification).Return(id);
Assert.IsTrue(((object)foo1).Equals(foo2));
}
[TestMethod]
public void BaseFoo_object_Equals_为具有相同识别号的_Foos_返回_true_
{
var id=“testId”;
var foo1=MockRepository.GenerateStub();
var foo2=MockRepository.GenerateStub();
foo1.存根(x=>x.Identification).返回(id);
foo2.存根(x=>x.Identification).返回(id);
Assert.IsTrue(((object)foo1.Equals(foo2));
}
当然,在我发布问题后我马上想到了
我不知道这是否是正确的方法,但它似乎起了作用。我告诉了存根。CallOriginalMethod()
[TestMethod]
public void BaseFoo_object_Equals_为具有相同识别号的_Foos_返回_true_
{
var id=“testId”;
var foo1=MockRepository.GenerateStub();
var foo2=MockRepository.GenerateStub();
foo1.存根(x=>x.Identification).返回(id);
foo2.存根(x=>x.Identification).返回(id);
foo1.Stub(x=>((object)x).Equals(Arg.Is.Anything)).CallOriginalMethod(OriginalCallOptions.noexpection);
Assert.IsTrue(((object)foo1.Equals(foo2));
}
[TestMethod]
public void BaseFoo_object_Equals_returns_true_for_Foos_with_same_Identification()
{
var id = "testId";
var foo1 = MockRepository.GenerateStub<BaseFoo>();
var foo2 = MockRepository.GenerateStub<BaseFoo>();
foo1.Stub(x => x.Identification).Return(id);
foo2.Stub(x => x.Identification).Return(id);
foo1.Stub(x => ((object)x).Equals(Arg<object>.Is.Anything)).CallOriginalMethod(OriginalCallOptions.NoExpectation);
Assert.IsTrue(((object)foo1).Equals(foo2));
}