模拟 C# Func 委托

发布于 2025-01-17 20:44:05 字数 1385 浏览 4 评论 0原文

我有一个问题,我有一个使用Func代表解决的依赖性,在这种情况下如何使用MOQ?

 public Func<string, IMylogic> MyLogic { get; set; }

定义是这样的:

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {           
        builder.Services.AddTransient(Factory);
    }

    private static Func<IServiceProvider, Func<string, IMyLogic>> Factory =>
    service =>
    {
        return key =>
        {
            return new MyLogic(key, new System.Net.Http.HttpClient());
        };
    };
}

这只是测试应如何工作的想法:

public class ExecuteEmitLogicTest
{
    ExecuteTransactionCommand ExecuteEmitCommand;
    Dictionary<string, StringValues> queryString;
    Mock<Func<string, IMyLogic>> Concrete;
       
    [Fact]
    public async Task ExecuteEmit()
    { 
        var Concrete = mockFactory.Create<Func<string, IMyLogic>>(MockBehavior.Loose);
        Concrete.Setup(c => c.Invoke(ConcreteTInfo).Execute(request, Guid.Parse(sessionId))).Returns(Task.FromResult(pol));
        ExecuteEmitCommand = new ExecuteTransactionCommand(Concrete.Object);
        var response = await ExecuteEmitCommand.ExecuteAndEmit(queryString, ConcreteTInfo.ApiSkrUrl, ConcreteTInfo.SkrKey, FIXED_HASH);
        Assert.True(response.IsValid);
    }
}

I have a question, I have a dependency that is resolved using the Func delegate, how can I use moq in this scenario?

 public Func<string, IMylogic> MyLogic { get; set; }

The definition is this:

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {           
        builder.Services.AddTransient(Factory);
    }

    private static Func<IServiceProvider, Func<string, IMyLogic>> Factory =>
    service =>
    {
        return key =>
        {
            return new MyLogic(key, new System.Net.Http.HttpClient());
        };
    };
}

This is just an idea on how the test should work:

public class ExecuteEmitLogicTest
{
    ExecuteTransactionCommand ExecuteEmitCommand;
    Dictionary<string, StringValues> queryString;
    Mock<Func<string, IMyLogic>> Concrete;
       
    [Fact]
    public async Task ExecuteEmit()
    { 
        var Concrete = mockFactory.Create<Func<string, IMyLogic>>(MockBehavior.Loose);
        Concrete.Setup(c => c.Invoke(ConcreteTInfo).Execute(request, Guid.Parse(sessionId))).Returns(Task.FromResult(pol));
        ExecuteEmitCommand = new ExecuteTransactionCommand(Concrete.Object);
        var response = await ExecuteEmitCommand.ExecuteAndEmit(queryString, ConcreteTInfo.ApiSkrUrl, ConcreteTInfo.SkrKey, FIXED_HASH);
        Assert.True(response.IsValid);
    }
}

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

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

发布评论

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

评论(1

浮生未歇 2025-01-24 20:44:05

我不了解使用&lt; string,Interface&gt;的重定向部分。功能,但我想测试看起来更像是这样:

public interface IMyLogic { int GetX(); }
...

Mock<IMyLogic> mockLogic = new Mock<IMyLogic>(MockBehavior.Strict);
// For the unit test always return the mock as 
// we are *not* testing the IMyLogic provider function
Func<string, IMyLogic> f = (string _) => mockLogic.Object;

mockLogic.Setup(ml => ml.GetX()).Returns(7);

var tested = new ExecuteTransactionCommand(f);
var response = await tested.ExecuteAndEmit(queryString, ConcreteTInfo.ApiSkrUrl, ConcreteTInfo.SkrKey, FIXED_HASH);
// Asserts here

I don't understand the redirection part with the <string, interface> function, but I would imagine the test to look more like this:

public interface IMyLogic { int GetX(); }
...

Mock<IMyLogic> mockLogic = new Mock<IMyLogic>(MockBehavior.Strict);
// For the unit test always return the mock as 
// we are *not* testing the IMyLogic provider function
Func<string, IMyLogic> f = (string _) => mockLogic.Object;

mockLogic.Setup(ml => ml.GetX()).Returns(7);

var tested = new ExecuteTransactionCommand(f);
var response = await tested.ExecuteAndEmit(queryString, ConcreteTInfo.ApiSkrUrl, ConcreteTInfo.SkrKey, FIXED_HASH);
// Asserts here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文