gpt4 book ai didi

c# - 除非明确定义,否则System.Action在单元测试中不知道

转载 作者:行者123 更新时间:2023-12-02 10:47:17 31 4
gpt4 key购买 nike

我遇到了一个奇怪的问题,即System.Action在上面未明确定义时无法解决。为了更好地解释这一点,我向您展示了代码

    [TestMethod]
public void Channel_Dispatch_Waits_Before_Return()
{
//Action useless = null;

WorkflowChannel channel = new WorkflowChannel();
bool isHandeled = false;

channel.Subscribe(WorkflowHandlerFactory.FromLambdaAsync<int>((data, callback, onError) =>
{
// Go on another thread and sleep for a small ammount of time to simulate a async request
System.Threading.ThreadPool.QueueUserWorkItem(state =>
{
System.Threading.Thread.Sleep(100);

isHandeled = true;

callback();
});
}));


channel.Dispatch(1);

Assert.IsTrue(isHandeled);
}

这给出了编译时错误,即:

Error 1 Delegate 'Action' does not take 0 arguments C:\Users***\Documents\Visual Studio 2010\Projects\MessageWorkflow\MessageWorkfow.Test\WorkflowChannelTest.cs 47 21 MessageWorkflow.Test



但是,当我取消注释该行时:
Action useless = null;

它不会给出编译时错误。我试图重建等,但没有用。
如果需要,FromLambdaAsync的声明为:
public static IWorkflowHandler FromLambdaAsync<T>(AsyncAction<T> method)

其中AsyncAction是:
public delegate void AsyncAction<T>(T message, Action onComplete, Action<Exception> onError);

它不是一帆风顺的,但我很困惑,因为我在这背后找不到任何逻辑……谁能打我一巴掌?

干杯!

*编辑*

我设法在最简单的测试用例中重现了此行为,请参见:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary2
{
public delegate void AsyncAction(Action onComplete);

public class WorkflowHandler
{

}

public class Main
{
public static WorkflowHandler FromLambdaAsync(AsyncAction method)
{
return new WorkflowHandler();
}
}
}

与单元测试:
using ClassLibrary2;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace TestProject1
{
[TestClass]
public class MainTest
{
[TestMethod()]
public void FromLambdaAsyncTest()
{
Main.FromLambdaAsync((callback) =>
{
callback();
});
}
}
}

这给了我同样的错误

该项目针对.NET 4.0 usign VS 2010 sp1进行编译

*编辑2:*

进一步简化了测试用例,结果没有变化

*编辑3 *

即使在最简单的测试用例中,此操作也会失败,请参见:
[TestMethod()]
public void FromLambdaAsyncTest()
{
AsyncAction action = c => c();
}

最佳答案

关于c# - 除非明确定义,否则System.Action在单元测试中不知道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5465565/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com