使用 Moq 通过任意键和值设置任意

发布于 2024-11-14 16:58:59 字数 988 浏览 4 评论 0原文

在问题的最后: 在 C# 中使用 Moq 设置索引器,出现了一个问题,有人强调了我也遇到的一个问题。但他们没有找到解决办法。

具体来说,我尝试使用通用 It.IsAny 作为键的匹配器,并通过 It.IsAny设置值。当通过索引访问并设置值时,它永远不会匹配,并且不会访问我的回调方法。所以我的单元测试失败了。

var stateTable = new HashTable;
var httpSession = new Mock<HttpSessionStateBase>();

//works via httpSession.Add(key, value);
httpSession.Setup(x => x.Add(It.IsAny<string>(), It.IsAny<object>()))
    .Callback((string index, object value) => {
        var i = index;
        var v = value;

            stateData[i] = v;
    });

//does not work via httpSession[key] = value;
httpSession.SetupSet(x => x[It.IsAny<string>()] = It.IsAny<object>())
    .Callback( (string index, object value) => {
        var i = index;
        var v = value;

        stateData[i] = v;
});

我正在使用起订量 4.0.10827

At the end of the question:
Using Moq to set indexers in C#, there was an issue that someone highlighted a problem that I am having as well. But they did not find a solution.

Specifically, I'm trying to use the generic It.IsAny<string> as the matcher for the key and setting the value via It.IsAny<object>. When accessing via an index and setting the value, it never matches and it does not access my call back method. And so my unit tests are failing.

var stateTable = new HashTable;
var httpSession = new Mock<HttpSessionStateBase>();

//works via httpSession.Add(key, value);
httpSession.Setup(x => x.Add(It.IsAny<string>(), It.IsAny<object>()))
    .Callback((string index, object value) => {
        var i = index;
        var v = value;

            stateData[i] = v;
    });

//does not work via httpSession[key] = value;
httpSession.SetupSet(x => x[It.IsAny<string>()] = It.IsAny<object>())
    .Callback( (string index, object value) => {
        var i = index;
        var v = value;

        stateData[i] = v;
});

I'm using Moq 4.0.10827

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

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

发布评论

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

评论(3

平安喜乐 2024-11-21 16:58:59

根据我的经验,这永远不会起作用,您不能使用 It.IsAny 作为索引器表达式中的匹配器。但是,如果您在索引器中输入具体值,它将匹配。
例如,以下内容确实有效:

httpSession.SetupSet(x => x["someValue"] = It.IsAny<object>())
    .Callback( (string index, object value) => {
        var i = index;
        var v = value;

        stateData[i] = v;
});

In my experience this never works, you cannot use the It.IsAny as a matcher in the indexer expression. However, it will match if you put a concrete value in the indexer.
For example, the following does work:

httpSession.SetupSet(x => x["someValue"] = It.IsAny<object>())
    .Callback( (string index, object value) => {
        var i = index;
        var v = value;

        stateData[i] = v;
});
断桥再见 2024-11-21 16:58:59

这就是我为验证阅读所做的事情。

_httpSessionStateBaseMock.VerifySet(x => x["keyname"] = It.IsAny<YourObject>(), Times.Once());

对于我刚刚阅读的内容

_httpSessionStateBaseMock.Verify(x => x["keyname"],Times.Once());

This is what I did to verify the read.

_httpSessionStateBaseMock.VerifySet(x => x["keyname"] = It.IsAny<YourObject>(), Times.Once());

for the reads I just went with

_httpSessionStateBaseMock.Verify(x => x["keyname"],Times.Once());
美男兮 2024-11-21 16:58:59

我不喜欢它,但我可以使用新的继承类来解决它。
然后,我重写索引器并调用可以模拟的虚拟方法。

public class HttpSessionStateBaseProxy : HttpSessionStateBase
{
    public virtual void SetItem(string key, object value)
    {
        base[key] = value;
    }
    public override object this[string name]
    {
        get
        {
            return base[name];
        }
        set
        {
            SetItem (name, value);
        }
    }

}

嘲笑是通过以下方式完成的:

Mock<HttpSessionStateBaseProxy> Session = new Mock<HttpSessionStateBaseProxy>(MockBehavior.Loose);
Session.CallBase = true;
Session.Setup(x => x.SetItem(It.IsAny<string>(),It.IsAny<object>()))
        .Callback<string,object>((string index, object value) =>
        {
            if (!this.SessionItems.Contains(index)) this.SessionItems.Add(index, value);
            else this.SessionItems[index] = value;


        });

        Session.Setup(s => s[It.IsAny<string>()]).Returns<string>(key =>
        {

            return this.SessionItems[key];

        });

I don't like it but I was able to work around it using a new inherits class.
I then override the indexer and called a virtual method which can be mocked.

public class HttpSessionStateBaseProxy : HttpSessionStateBase
{
    public virtual void SetItem(string key, object value)
    {
        base[key] = value;
    }
    public override object this[string name]
    {
        get
        {
            return base[name];
        }
        set
        {
            SetItem (name, value);
        }
    }

}

and the mocking is done by :

Mock<HttpSessionStateBaseProxy> Session = new Mock<HttpSessionStateBaseProxy>(MockBehavior.Loose);
Session.CallBase = true;
Session.Setup(x => x.SetItem(It.IsAny<string>(),It.IsAny<object>()))
        .Callback<string,object>((string index, object value) =>
        {
            if (!this.SessionItems.Contains(index)) this.SessionItems.Add(index, value);
            else this.SessionItems[index] = value;


        });

        Session.Setup(s => s[It.IsAny<string>()]).Returns<string>(key =>
        {

            return this.SessionItems[key];

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