在QML中,在某些情况下,我需要将旧方法移除到一个信号并重新定义一个新方法来处理该信号,演示如下:
import QtQuick 2.6
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
MouseArea {
id:mouse
anchors.fill: parent
onClicked: {
console.log("11111");
}
}
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
Component.onCompleted: {
//if(false){
//}
// mouse.clicked = null;
//mouse.clicked.disconnect();
mouse.clicked.connect(
function(){
console.log("22222")
});
}
}
Run Code Online (Sandbox Code Playgroud)
我连接了一个新函数,但是它会同时显示"11111" 和 "22222",似乎它添加了一个新函数来处理信号。
我试过设置“ mouse.clicked = null ”,还是不行。
我也尝试断开连接方法,如下所示:
MouseArea {
id:mouse
anchors.fill: parent
onClicked:say()
}
Component.onCompleted: {
mouse.clicked.disconnect(say);
mouse.clicked.connect(
function(){
console.log("22222")
});
}
function say(){
console.log("11111"); …Run Code Online (Sandbox Code Playgroud) 我想设置800ms的延迟时间来运行一个函数,所以我用一个计时器来处理它.代码如下.但是我发现,第一次,函数运行正常,它只显示一个console.log("这里是控制台....."); ,但是当我再次点击它时,它显示2个控制台,在第三次点击时,它显示3个控制台,依此类推......
我无法理解为什么会发生这种情况,任何朋友都可以为我解释一下吗?
import QtQuick 2.6
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
MouseArea {
anchors.fill: parent
onClicked: {
delayTimer(800,function(){
console.log("here is console.....");
var t= Math.random();
console.log(t);
})
}
}
Timer{
id:dtimer
}
function delayTimer(delayTime, cb) {
console.log("delayTimer is starting");
dtimer.interval = delayTime;
dtimer.repeat = false;
dtimer.triggered.connect(cb);
dtimer.start();
}
}
Run Code Online (Sandbox Code Playgroud)
点击几下,当我再次点击它时,输出是:
qml: delayTimer is starting
qml: here is console.....
qml: 0.27777099609375
qml: here is console.....
qml: 0.407012939453125
qml: here is console.....
qml: …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过 Moq 在我的项目(.net core 3.1)中测试 ILogger(Microsoft.Extensions.Logging),但是...它失败了..我试图捕获代码中的异常:
try{
//do something
}
catch (Exception e)
{
_logger.LogError(new
{
RequestId = requestId.ToString(),
Topic = "Merge files",
Message = "process failed",
Status = "Failed"
}, e);
}
Run Code Online (Sandbox Code Playgroud)
我定义了模拟记录器
private readonly Mock<ILogger<MyClass>> _logger = new Mock<ILogger<MyClass>>();
Run Code Online (Sandbox Code Playgroud)
这是我的 UT
_logger.Verify(
x => x.Log(
It.IsAny<LogLevel>(),
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, t) => true),
It.IsAny<Exception>(),
It.Is<Func<It.IsAnyType, Exception, string>>((v, t) => true)), Times.Once);
Run Code Online (Sandbox Code Playgroud)
我调试了它,在我的代码中执行了日志异常方法,但最后 UT 失败,因为我收到以下消息:
预期在模拟上调用一次,但实际调用次数为 0 次: x => x.Log<It.IsAnyType>(It.IsAny(), It.IsAny(), It.Is<It.IsAnyType>((v, t) => True), It.IsAny(), It.Is<Func<It.IsAnyType, Exception, string>>((v, t) …
我想在我的 UT 中测试我的 httpclient post 重试功能,这里我模拟了 HttpFactory:
第一次和第二次,HttpFactory 总是返回 HttpStatusCode.InternalServerError
public class MyServiceClient
{
private readonly IHttpClientFactory _clientFactory;
public MyServiceClient(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
public async Task<string> GetResponse(string test= "te")
{
using var client = _clientFactory.CreateClient("MyClient");
var content = new StringContent("{}", Encoding.UTF8, "application/json");
var response = await client.PostAsync("http://www.contoso.com/",content);
if (!response.IsSuccessStatusCode)
{
throw new ApplicationException("Application Error!");
}
var result = await response.Content.ReadAsStringAsync();
return result;
}
public async Task<string> PollyExecute()
{
try
{
var policy = Policy
.Handle<Exception>()
.WaitAndRetryAsync(3,
count …Run Code Online (Sandbox Code Playgroud)