Serverspec 支持预期还是我必须使用 should?
Does Serverspec support expectations or do I have to use should?
我听说我应该在 Serverspec
中使用期望而不是 "should" 语句
我一直在谷歌上搜索可用于文件匹配的期望,但我看到的所有关于 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
那么我该如何使用 expectations 而不是 should 来编写测试呢?
你的第一个问题:
... all of the tutorials I see for Serverspec use should rather than expect. Is this because Serverspec hasn't been updated to use expectations?
不,这主要是因为 Serverspec 项目的作者偏爱 "should" 语法,这就是 Serverspec 文档继续使用它的原因。他解释 here 是:
I'm using should
syntax instead of expect
syntax because I think should
syntax is more readable than expect
syntax and I like it.
Using expect
syntax is recommended way because adding should
to every object causes failures when used with BasicObject-subclassed proxy objects.
But the one-liner syntax used with the examples in this page doesn't add should to any objects, so this syntax doesn't cause the above problems. That's why I'm using the one-liner should syntax.
请注意 should
和 expect
来自 rspec-expectations 项目,Serverspec 作者是正确的 "should" 而不是 "expect" 很好他使用它的方式。
Rspec 作者 Myron Marston here 提供了有关 expect 语法最初动机的更多信息。
你的第二个问题:
... how would I write the test using expectations instead of should?
如果您仍想使用 expect
语法,只需将所有地方的 should
替换为 is_expected.to
。这很好用:
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
另请参阅:
- should and should_not syntax(rspec-expectations 项目)。
我听说我应该在 Serverspec
中使用期望而不是 "should" 语句我一直在谷歌上搜索可用于文件匹配的期望,但我看到的所有关于 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
那么我该如何使用 expectations 而不是 should 来编写测试呢?
你的第一个问题:
... all of the tutorials I see for Serverspec use should rather than expect. Is this because Serverspec hasn't been updated to use expectations?
不,这主要是因为 Serverspec 项目的作者偏爱 "should" 语法,这就是 Serverspec 文档继续使用它的原因。他解释 here 是:
I'm using
should
syntax instead ofexpect
syntax because I thinkshould
syntax is more readable thanexpect
syntax and I like it.Using
expect
syntax is recommended way because addingshould
to every object causes failures when used with BasicObject-subclassed proxy objects.But the one-liner syntax used with the examples in this page doesn't add should to any objects, so this syntax doesn't cause the above problems. That's why I'm using the one-liner should syntax.
请注意 should
和 expect
来自 rspec-expectations 项目,Serverspec 作者是正确的 "should" 而不是 "expect" 很好他使用它的方式。
Rspec 作者 Myron Marston here 提供了有关 expect 语法最初动机的更多信息。
你的第二个问题:
... how would I write the test using expectations instead of should?
如果您仍想使用 expect
语法,只需将所有地方的 should
替换为 is_expected.to
。这很好用:
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
另请参阅:
- should and should_not syntax(rspec-expectations 项目)。