首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Serverspec是否支持预期,还是必须使用have?

Serverspec是否支持预期,还是必须使用have?
EN

Stack Overflow用户
提问于 2019-05-22 10:40:02
回答 1查看 205关注 0票数 0

我听说我应该使用预期而不是Serverspec中的“应该”语句。

我一直在搜索可以用于文件匹配的期望,但是我看到的关于Serverspec使用的所有教程都应该而不是预期的。这是因为Serverspec还没有更新以使用预期吗?

代码语言:javascript
复制
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

那么,我如何使用期望值而不是应该来编写测试呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-22 10:54:58

你的第一个问题:

..。我看到的关于Serverspec使用的所有教程都应该而不是预期的。这是因为Serverspec还没有更新以使用预期吗?

不,这主要是因为Serverspec项目的作者偏爱“应该”语法,这就是为什么Serverspec文档继续使用它的原因。他向这里解释说:

我使用的是should语法,而不是expect语法,因为我认为should语法比expect语法更具可读性,我喜欢它。 建议使用expect语法,因为向每个对象添加should会导致与BasicObject子类代理对象一起使用时的失败。 但是,与本页中的示例一起使用的一行语法不应该添加到任何对象中,因此这种语法不会导致上述问题。这就是为什么我使用一行应该语法的原因。

请注意,shouldexpect来自rspec-期望项目,而Serverspec作者是正确的“应该”而不是“期望”,他使用它的方式很好。

Rspec作者Marston 这里提供了关于expect语法的原始动机的更多信息。

你的第二个问题:

..。我将如何使用期望值而不是应该来编写测试?

如果仍然想使用expect语法,只需将should everywhere替换为is_expected.to everywhere。这样做很好:

代码语言:javascript
复制
describe file("/etc/passwd") do
  it { is_expected.to be_file }
  its(:content) { is_expected.to match /root/ }
end

你也可以这样写:

代码语言:javascript
复制
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

甚至:

代码语言:javascript
复制
describe file("/etc/passwd") do
  it { expect(subject).to be_file }
  it { expect(subject.content).to match /root/ }
end

另请参阅:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56254952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档