ServerPec支持期望或我必须使用



我听说我应该使用期望,而不是"应该"在servervece

我一直在谷歌搜索,以搜索可用于文件匹配的期望,但是我看到的所有教程用于serververpecpec,而不是期望。这是因为ServerPec尚未更新以使用期望吗?

describe file('/etc/yum.conf') do
  it { should be_file }
  it { should be_owned_by 'root' }
  it { should be_grouped_into 'root' }
  it { should be_mode 644 }
  its(:content) { should match /^gpgcheck=1$/ }
  its(:content) { should match /^clean_requirements_on_remove=1$/ }
end

那么,如何使用期望而不是应该写测试?

您的第一个问题:

...我看到的所有教程用于" erververpec"使用应该而不是期望。这是因为ServerPec尚未更新以使用期望吗?

否,这主要是因为ServerSpec项目的作者偏爱"应该"语法,这就是为什么serververpec文档继续使用它的原因。他在这里解释说:

我使用的是should语法而不是expect语法,因为我认为should语法比expect语法更可读,我喜欢它。

建议使用expect语法,因为将should添加到每个对象中会导致失败,与BasicObject-sublagersed代理对象一起使用。

但是,本页中的示例中使用的单线语法不应添加到任何对象,因此该语法不会引起上述问题。这就是为什么我使用单线应该语法。

请注意, shouldexpect来自RSPEC-HEPPECT项目,而serververpec作者是正确的"应该"而不是"期望",就像他使用的方式一样。

有更多有关RSPEC作者Myron Marston的期望语法原始动机的信息。

您的第二个问题:

...如何使用期望而不是应该写测试?

如果您仍然想使用expect语法,则只需将is_expected.to替换为到处 CC_11。这很好:

describe file("/etc/passwd") do
  it { is_expected.to be_file }
  its(:content) { is_expected.to match /root/ }
end

您也可以这样写:

describe "passwd file" do
  it "the passwd file should be a file" do
    expect(file("/etc/passwd")).to be_file }
  end
  it "and it should contain root" do
    expect(file("/etc/passwd").content).to match /root/
  end
end

甚至:

describe file("/etc/passwd") do
  it { expect(subject).to be_file }
  it { expect(subject.content).to match /root/ }
end

另请参见:

最新更新