0

我有 VS2012 专业版,非常希望我经过精心打磨的测试能够在构建结束时运行。

我想我可以通过简单地编写一个插件来做到这一点。所以我在 ac# 插件中有这个。

public void OnConnection(object app, ext_ConnectMode cM, object aI, ref Array cust) {
    _applicationObject = (DTE2)app;
    _addInInstance = (AddIn)aI;
    if (_applicationObject != null) {
        _bldevents = _applicationObject.Events.BuildEvents;
        _bldevents.OnBuildDone += _bldevents_OnBuildDone;
    }
}

void _bldevents_OnBuildDone(vsBuildScope Scope, vsBuildAction Action) {
    try {
        _applicationObject.ExecuteCommand("TestExplorer.RunAllTests");
    } catch(Exception e) {
        string d = " + " + e.HResult;
        Clipboard.SetText(e.Message + " ~ " + e.HResult);
        MessageBox.Show(e.Message);
    }
}

但是,当我进行构建时,我收到错误Error HRESULT E_FAIL has been returned from a call to a COM 组件。~ -2147467259

ExecuteCommand 处理 File.NewFile 之类的东西,在命令窗口中运行 TestExplorer.RunAllTests 没有问题。

是否有一些设置需要做或让 ms 以某种方式阻碍了这种行为,因为他们希望我破产并获得最终版本;)

有什么想法吗?

4

1 回答 1

0

It looks like TestExplorer.RunAllTests performs build first, your _bldevents_OnBuildDone is called again, TestExplorer.RunAllTests is called again and second time it throws the exception.

You can add the IsCommandAvailable check to prevent this recursion:

if (IsCommandAvailable("TestExplorer.RunAllTests"))
                _applicationObject.ExecuteCommand("TestExplorer.RunAllTests");

bool IsCommandAvailable(string command) 
{
    Commands2 commands = (Commands2)(_applicationObject.Commands);
    if (commands == null)
        return false;

    Command dte_command = commands.Item(command, 0);
    if (dte_command == null)
        return false;

    return dte_command.IsAvailable;
}
于 2013-09-01T07:22:09.913 回答