FakeItEasy - problems with new modifier

北城以北 提交于 2019-12-11 11:49:10

问题


It appears that the following code doesn't behave as I would expect:

using FakeItEasy;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        var derived = A.Fake<IDerived>();
        A.CallTo(() => derived.Dependency).Returns(null);

        IBase baseObj = derived;
        Assert.IsNull(baseObj.Dependency); //Fails
    }
}

public interface IDerived : IBase
{
    new IDependency Dependency { get; }
}

public interface IBase
{
    IDependency Dependency { get; }
}

public interface IDependency
{
}

Instead of returning null, fake it easy returns a fake instance of IDependency. Perhaps by design? Anyway, how would I go around this problem and ensure baseObj.Dependency returns what was configured?


回答1:


This is normal behavior. IDerived now has two members. One inherited from IBase and one defined in IDerived.

In your test method, your are using FakeItEasy to set the value of the one in IDerived.

The member from IBase was not set. So it get the default value that FakeItEasy gives it which is a mocked IDependency.

If you want to set it, use the following code:

IDerived derived = A.Fake<IDerived>();

IBase baseObj = derived;

A.CallTo(() => baseObj.Dependency).Returns(null);

Assert.IsNull(baseObj.Dependency); //No error here

In this code, we are setting the other member of IDerived, which is the dependency defined in the base interface.



来源:https://stackoverflow.com/questions/32405776/fakeiteasy-problems-with-new-modifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!