小编Dan*_*n R的帖子

RSpec - 如何复合匹配器 raise_error 和 output.to_stdout?

我有一个基于 Thor 的 ruby​​ CLI 项目,我正在尝试向其中添加单元测试。它最初是一个宠物项目,现在在内部相当大且重要。不管怎样,我从来没有真正开始过单元测试,只是移植到现有的例子上,所以当谈到 rspec 时我是一个菜鸟。

我从一个相当基本的示例开始,确保如果我给它一个不存在的文件,它会回复一个解释性错误并退出。

以下是我尝试将它们复合的方法:

context 'with incorrect filename' do
  it 'should fail cleanly' do
    expect do
      subject.format('bad_file_name')
    end.to output('   ERROR  Unable to load file bad_file_name, exiting...').to_stdout.and raise_error(SystemExit)
  end
end
Run Code Online (Sandbox Code Playgroud)

它可以工作,但不能在这里正确捕获标准输出输出..也尝试过:

context 'with incorrect filename' do
  it 'should fail cleanly' do
    expect do
      expect do
        subject.format('fixtures/invalid.yaml')
      end.to output('   ERROR  Unable to load file bad_file_name, exiting...').to_stdout
    end.to raise_error(SystemExit)
  end
end
Run Code Online (Sandbox Code Playgroud)

看起来它是有效的,除了(如本例所示)它实际上并没有测试输出,因为输出不匹配。

那么同时检查引发的错误和标准输出的输出的正确方法是什么?

ruby rspec thor

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

Powershell:为什么写输出和写警告对字符串和变量的处理方式不同?

我对powershell比较陌生,我发现一个奇怪的是我不确定如何解释.

这输出就好了.

write-output $var.Name.padright(40)" - "(get-date -format s)" : Creating on $var1"
Run Code Online (Sandbox Code Playgroud)

另一方面,这会抛出一个错误:

write-warning $var.Name.padright(40)" - "(get-date -format s)" : Creating on $var1"

Write-Warning : A positional parameter cannot be found that accepts argument ' - '.
At Y:\test.ps1:228 char:22
+         write-warning <<<<  $var.name.padright(40)" - "(get-date -format s)" : Creating on $var1"
+ CategoryInfo          : InvalidArgument: (:) [Write-Warning], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WriteWarningCommand
Run Code Online (Sandbox Code Playgroud)

知道为什么吗?如果我想完成上述工作,我会这样做:

$warning = $var.Name.padright(40) + " - " + (get-date -format s) + " : Creating on …
Run Code Online (Sandbox Code Playgroud)

powershell

3
推荐指数
1
解决办法
3523
查看次数

Powershell:使用命名空间向 XML 文档添加元素

我正在尝试编写一个使用 VMWare 的 vcloud REST API 添加 NIC 的 powershell cmdlet(VMWare 自己的 powerCLI 似乎不允许这样做)。为此,我需要获取此 XML:

<?xml version="1.0" encoding="UTF-8"?><vcloud:RasdItemsList
    xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData"
    xmlns:vcloud="http://www.vmware.com/vcloud/v1.5"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    href="https://vcloud.example.com/api/vApp/vm-89c84bd6-c6f2-4e4c-8a7d-c44a3489e2e4/virtualHardwareSection/networkCards"
    type="application/vnd.vmware.vcloud.rasdItemsList+xml">
    <vcloud:Link
        href="https://vcloud.example.com/api/vApp/vm-89c84bd6-c6f2-4e4c-8a7d-c44a3489e2e4/virtualHardwareSection/networkCards"
        rel="edit"
        type="application/vnd.vmware.vcloud.rasdItemsList+xml"/>
</vcloud:RasdItemsList>
Run Code Online (Sandbox Code Playgroud)

并在最后一个 vcloud:Link 下面添加以下元素。

    <ovf:Item>
        <rasd:Address></rasd:Address>
        <rasd:AddressOnParent>0</rasd:AddressOnParent>
        <rasd:AutomaticAllocation>false</rasd:AutomaticAllocation>
        <rasd:Connection
            vcloud:ipAddressingMode="none"
            vcloud:primaryNetworkConnection="true">VM Network</rasd:Connection>
        <rasd:Description>E1000 ethernet adapter on "VM Network"</rasd:Description>
        <rasd:ElementName>Network adapter 0</rasd:ElementName>
        <rasd:InstanceID>1</rasd:InstanceID>
        <rasd:ResourceSubType>E1000</rasd:ResourceSubType>
        <rasd:ResourceType>10</rasd:ResourceType>
    </ovf:Item>
Run Code Online (Sandbox Code Playgroud)

现在,我已将原始 XML 作为 $xmlDoc 导入 PS,并且尝试了以下代码:

$xmlElt = $xmlDoc.CreateElement("ovf:Item")
$xmlSubElt = $xmlDoc.CreateElement("rasd:Address")
$xmlElt.AppendChild($xmlSubElt)
$xmlSubElt = $xmlDoc.CreateElement("rasd:AddressOnParent")
$xmlSubText = $xmlDoc.CreateTextNode("0")
$xmlSubElt.AppendChild($xmlSubText)
$xmlElt.AppendChild($xmlSubElt)
$xmlSubElt = $xmlDoc.CreateElement("rasd:AutomaticAllocation")
$xmlSubText = $xmlDoc.CreateTextNode("false")
$xmlSubElt.AppendChild($xmlSubText) …
Run Code Online (Sandbox Code Playgroud)

xml powershell vmware xml-parsing vcloud-director-rest-api

2
推荐指数
1
解决办法
7051
查看次数