小编kol*_*man的帖子

如何使用Moq模拟接口的`object.Equals(object obj)`

我有一个有趣的问题包围你的头.考虑一下这样的界面:

public interface IMyThing
{
    int Id { get; }
}
Run Code Online (Sandbox Code Playgroud)

现在我想测试使用此接口的代码.也许有一些LINQ魔术.像这样的东西:

public class SomeClass
{
    private IMyThing _thing;

    ...

    public bool HasThing(IEnumerable<IMyThing> things)
    {
        return things.Contains(_thing);
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在嘲笑所有IMyThing使用的对象Moq:

public static IMyThing MockMyThing(int newId)
{
    var mock = new Mock<IMyThing>();
    mock.Setup(s => s.Id).Returns(newId);
    mock.Setup(s => s.Equals(It.IsAny<object>())).Returns<object>(obj =>
    {
        if (typeof(IMyThing).IsAssignableFrom(obj.GetType()))
        {
            return ((IMyThing)obj).Id == newId;
        }

        return false;
    });

    return mock.Object;
}
Run Code Online (Sandbox Code Playgroud)

这就是事情.上面的代码编译没有警告,但永远不会工作.MoqEquals()方法创建一个拦截器,但永远不会到达.而是调用对象代理的equals方法.我谴责的事实是我在嘲笑一个界面而不是一个具体的类.

更新:刚刚意识到Moq甚至没有创建一个拦截器.

当然我可以IMyThing像这样扩充界面:

public …
Run Code Online (Sandbox Code Playgroud)

.net c# moq

10
推荐指数
1
解决办法
1425
查看次数

使用PyObjC与Mountain Lion的通知中心合作

我正在尝试从我的python脚本向Mountain Lion发送通知,并对通知的点击做出反应.现在可以完美地发送通知.但是,我无法让Lion在点击后回拨我的剧本.

这就是我的工作.我实现了一个Notification类.该类实例的唯一目的是通过调用提供通知notify().在同一方法中,我将对象设置为app的委托.

import Foundation
import objc
import AppKit

class MountainLionNotification(Foundation.NSObject, Notification):

    def notify(self, title, subtitle, text, url):
        NSUserNotification = objc.lookUpClass('NSUserNotification')
        NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
        notification = NSUserNotification.alloc().init()
        notification.setTitle_(str(title))
        notification.setSubtitle_(str(subtitle))
        notification.setInformativeText_(str(text))
        notification.setSoundName_("NSUserNotificationDefaultSoundName")
        notification.setUserInfo_({"action":"open_url", "value":url})
        AppKit.NSApplication.sharedApplication().setDelegate_(self)
        NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)

    def applicationDidFinishLaunching_(self, sender):
        userInfo = sender.userInfo()
        if userInfo["action"] == "open_url":
            import subprocess
            subprocess.Popen(['open', userInfo["value"]])
Run Code Online (Sandbox Code Playgroud)

现在我希望applicationDidFinishLaunching_()在点击通知时被调用.不幸的是,从未发生过 我究竟做错了什么?

python pyobjc osx-mountain-lion nsusernotification

8
推荐指数
1
解决办法
3889
查看次数

如何在 C++/Cli 中使用 awaitable

async/await在过去几年中在 C# 世界中获得了很大的人气。异步函数也倾向于在应用程序中快速传播:Awaitables 需要等待,因此调用函数必须是异步的,因此也是一个需要等待的 awaitable,等等。

我在 C++/Cli 项目中使用 C# 库。该库公开异步 API。Visual C++ 编译器不支持async/ await。因此,我无法等待库提供给我的任何 API。

我有以下选择:

  • 调用异步函数并“放手”:不是一个选项,因为我经常需要返回值才能继续。
  • 调用Wait()或访问异步函数返回ResultTask/Task<T>对象的属性:导致UI 线程臭名昭著的死锁

有什么办法可以使这项工作?如果必须的话,我不介意同步执行异步 API。

.net c# c++-cli async-await

5
推荐指数
1
解决办法
4301
查看次数

bool x =(A || B && C)

这个表达式是等同于((A || B) && C)还是对(A || (B && C))

例如:

设A = 1,B = 0,C = 0.这个表达的结果是什么?是0(案例1)还是1(案例2)?

c++ if-statement operator-precedence

-6
推荐指数
1
解决办法
4473
查看次数