我听说我应该使用预期而不是Serverspec中的“应该”语句。
我一直在搜索可以用于文件匹配的期望,但是我看到的关于Serverspec使用的所有教程都应该而不是预期的。这是因为Serverspec还没有更新以使用预期吗?
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那么,我如何使用期望值而不是应该来编写测试呢?
发布于 2019-05-22 10:54:58
你的第一个问题:
..。我看到的关于Serverspec使用的所有教程都应该而不是预期的。这是因为Serverspec还没有更新以使用预期吗?
不,这主要是因为Serverspec项目的作者偏爱“应该”语法,这就是为什么Serverspec文档继续使用它的原因。他向这里解释说:
我使用的是
should语法,而不是expect语法,因为我认为should语法比expect语法更具可读性,我喜欢它。 建议使用expect语法,因为向每个对象添加should会导致与BasicObject子类代理对象一起使用时的失败。 但是,与本页中的示例一起使用的一行语法不应该添加到任何对象中,因此这种语法不会导致上述问题。这就是为什么我使用一行应该语法的原因。
请注意,should和expect来自rspec-期望项目,而Serverspec作者是正确的“应该”而不是“期望”,他使用它的方式很好。
Rspec作者Marston 这里提供了关于expect语法的原始动机的更多信息。
你的第二个问题:
..。我将如何使用期望值而不是应该来编写测试?
如果仍然想使用expect语法,只需将should everywhere替换为is_expected.to everywhere。这样做很好:
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另请参阅:
https://stackoverflow.com/questions/56254952
复制相似问题