带有受保护设置器的最小起订量属性

发布于 2024-10-14 16:59:21 字数 604 浏览 1 评论 0原文

我想要 Moq 下一个对象:

abstract class Foo
{
    public string Bar { get; protected set; }
}

以便 new Mock().Bar 返回 "Blah"

我怎样才能做到这一点?


fooMock.SetupGet<string>(s => s.Bar).Returns("Blah");

投掷

失败:System.NotSupportedException:非虚拟成员上的设置无效:s => s.日期

fooMock.Protected().SetupGet<string>("Bar").Returns("Blah");

抛出

要指定公共属性 StatementSection.Date 的设置,请使用类型化重载

I want to Moq next object:

abstract class Foo
{
    public string Bar { get; protected set; }
}

so that new Mock<Foo>().Bar return "Blah".

How can I do that?


fooMock.SetupGet<string>(s => s.Bar).Returns("Blah");

throws

Failure: System.NotSupportedException : Invalid setup on a non-virtual member: s => s.Date

and

fooMock.Protected().SetupGet<string>("Bar").Returns("Blah");

throws

To specify a setup for public property StatementSection.Date, use the typed overloads

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剩一世无双 2024-10-21 16:59:21

就像 Felice (+1) 所说的那样,模拟创建了一个代理,这意味着您需要将事物虚拟化(这样 Moq 就可以发挥其代理魔力并覆盖该属性)。

作为替代方案,如果您只想注入一个值,您可以手动存根要测试的类并公开获取设置器的方法:-

public class FooStub : Foo {
    public SetBar(string newValue) {
       Bar = newValue;
    }
}

Like Felice (+1) said mocking creates a proxy which means you need to either make things virtual (so Moq can work its proxying magic and override the property).

As an alternative if you just want to squirt in a value you can manually stub the class you want to test and expose a means to get at the setter:-

public class FooStub : Foo {
    public SetBar(string newValue) {
       Bar = newValue;
    }
}
违心° 2024-10-21 16:59:21

由于模拟是通过创建类的代理来完成的,因此只有虚函数/属性可以被“moqued”

Since mocking is done by creating a proxy of your class,only virtual function/property can be "moqued"

余生再见 2024-10-21 16:59:21

下面是提供代理类的示例,该代理类可以通过其构造函数修改受保护的属性(MyClassMyClassProxy 代理)。测试是 XUnit。

public class MyClassEqualityComparerTests
{
    /// <summary>
    /// Normal class
    /// </summary>
    public class MyClass
    {
        public Guid Id { get; protected set; }
    }

    /// <summary>
    /// Proxy class facilitates setting of protected Id
    /// </summary>
    public class MyClassProxy : MyClass
    {
        public MyClassProxy(Guid id)
        {
            this.Id = id;
        }
    }

    public class MyClassEqualityComparer : IEqualityComparer<MyClass>
    {
        public bool Equals(MyClass x, MyClass y)
        {
            return x.Id.Equals(y.Id);
        }

        public int GetHashCode(MyClass obj)
        {
            return obj.Id.GetHashCode();
        }
    }

    [Fact]
    public void CompareSameMyClasss_ShouldBeEqual()
    {
        var sharedGuid = Guid.NewGuid();

        var myClass1 = new MyClassProxy(sharedGuid);
        var myClass2 = new MyClassProxy(sharedGuid);

        //arrange
        var myClassEqualityComparer = new MyClassEqualityComparer();

        //act
        bool equals = myClassEqualityComparer.Equals(myClass1, myClass2);

        //assert
        equals.Should().BeTrue();
    }

    [Fact]
    public void CompareDifferentMyClasss_ShouldNotBeEqual()
    {
        var myClass1 = new MyClassProxy(Guid.NewGuid());
        var myClass2 = new MyClassProxy(Guid.NewGuid());

        //arrange
        var myClassEqualityComparer = new MyClassEqualityComparer();

        //act
        bool equals = myClassEqualityComparer.Equals(myClass1, myClass2);

        //assert
        equals.Should().BeFalse();
    }
}

Here's an example of providing a proxy class that can modify the protected property via it's constructor (MyClass is proxied by MyClassProxy). Tests are XUnit.

public class MyClassEqualityComparerTests
{
    /// <summary>
    /// Normal class
    /// </summary>
    public class MyClass
    {
        public Guid Id { get; protected set; }
    }

    /// <summary>
    /// Proxy class facilitates setting of protected Id
    /// </summary>
    public class MyClassProxy : MyClass
    {
        public MyClassProxy(Guid id)
        {
            this.Id = id;
        }
    }

    public class MyClassEqualityComparer : IEqualityComparer<MyClass>
    {
        public bool Equals(MyClass x, MyClass y)
        {
            return x.Id.Equals(y.Id);
        }

        public int GetHashCode(MyClass obj)
        {
            return obj.Id.GetHashCode();
        }
    }

    [Fact]
    public void CompareSameMyClasss_ShouldBeEqual()
    {
        var sharedGuid = Guid.NewGuid();

        var myClass1 = new MyClassProxy(sharedGuid);
        var myClass2 = new MyClassProxy(sharedGuid);

        //arrange
        var myClassEqualityComparer = new MyClassEqualityComparer();

        //act
        bool equals = myClassEqualityComparer.Equals(myClass1, myClass2);

        //assert
        equals.Should().BeTrue();
    }

    [Fact]
    public void CompareDifferentMyClasss_ShouldNotBeEqual()
    {
        var myClass1 = new MyClassProxy(Guid.NewGuid());
        var myClass2 = new MyClassProxy(Guid.NewGuid());

        //arrange
        var myClassEqualityComparer = new MyClassEqualityComparer();

        //act
        bool equals = myClassEqualityComparer.Equals(myClass1, myClass2);

        //assert
        equals.Should().BeFalse();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文